精华内容
下载资源
问答
  • 通过Graphics2D生成正太对数分布图

    千次阅读 2016-12-12 14:35:38
    根据两组数组,生成柱状以及折线。 效果如下: 实现代码如下,注释描述的很详细了,就不讲解了: package com.byit.menu; import java.awt.Color; import java.awt.Graphics2D; import java.awt.geom....

          根据两组数组,生成柱状图以及折线图。 效果图如下:




    实现代码如下,注释描述的很详细了,就不讲解了:

    package com.byit.menu;
    
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Line2D;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.text.DecimalFormat;
    
    import javax.imageio.ImageIO;
    
    import com.byit.platform.util.PubUtil;
    
    public class AxisImage {
    	
    	private boolean hisFlag= true;
    	private boolean polyFlag= true;
    	
    	private int picWidth = 1024;
    	private int picHeight = 768;
    
    	/**
    	 * 坐标轴目前默认为图片的4分之三大小,并且居中
    	 */
    	private int axisw = picWidth * 3 / 4;// 坐标轴的宽度
    	private int axish = picHeight * 3 / 4;// 坐标轴的高度
    	private int axisx = (picWidth - axisw) / 2;// 坐标轴距离图片左侧边缘的距离
    	private int axisy = (picHeight - axish) / 2;// 坐标轴距离图片上方边距的距离
    
    	/**
    	 * 右上提示框坐标参数
    	 */
    	
    	private int promptw = 0; // 提示框宽度
    	private int prompth = 0; // 提示框高度
    	private int promptx1 = 0;// 提示框左上起点坐标
    	private int prompty1 = 0;
    
    	private int promptx2 = 0;// 提示框右下起点坐标
    	private int prompty2 = 0;
    
    	private int promptx3 = 0;// 提示框右下起点坐标
    	private int prompty3 = 0;// 提示框终点坐标
    
    	/**
    	 * 提示框内容参坐标数
    	 */
    	private int tipsLenth = 0;// 提示线长度
    	private int tipsx1 = 0;// 第一条提示内容起点坐标
    	private int tipsy1 =0;
    	private int tipsx1e = 0;// 终点坐标
    	private int tipsy1e = 0;// 终点坐标
    	private int tipsx2 = 0;// 第二条提示内容起点坐标
    	private int tipsy2 = 0;
    	private int tipsx2e = 0;// 终点坐标
    	private int tipsy2e = 0;// 终点坐标
    	private int axisDownSub = 15;//下标长度
    	private double oneElementWidthX=0;// 每个坐标X轴精度占据的长度
    	private double oneElementWidthY=0;// 每个坐标Y轴精度占据的长度
    	private static int hisArrLenth = 0 ;
    	private static int polyArrLenth = 0 ;
    	
    	private double[] histogramX = null; //柱状图X轴
    	private double[] histogramY = null; //柱状图Y轴
    	
    	private double[] polylineX = null; //折线图X轴
    	private double[] polylineY = null; //折线图Y轴
    	
    	
    	private BufferedImage bi = null;
    	private Graphics2D g2 = null;
    	private File file = null;
    
    	
    	public boolean isHisFlag() {
    		return hisFlag;
    	}
    	/**
    	 * 设置是否打印柱状图,默认为true
    	 * @param hisFlag
    	 */
    	public void setHisFlag(boolean hisFlag) {
    		this.hisFlag = hisFlag;
    	}
    	public boolean isPolyFlag() {
    		return polyFlag;
    	}
    	/**
    	 * 设置是否打印折线图,默认为true
    	 * @param polyFlag
    	 */
    	public void setPolyFlag(boolean polyFlag) {
    		this.polyFlag = polyFlag;
    	}
    	/**
    	 * 获取图片宽度
    	 * @return
    	 */
    	public int getPicWidth() {
    		return picWidth;
    	}
    	/**
    	 * 设置图片宽度,默认值为1024
    	 * @param picWidth
    	 */
    	public void setPicWidth(int picWidth) {
    		this.picWidth = picWidth;
    	}
    	/**
    	 * 获取图片高度
    	 * @return
    	 */
    	public  int getPicHeight() {
    		return picHeight;
    	}
    	/**
    	 * 设置图片高度,默认值为768
    	 * @param picHeight
    	 */
    	public  void setPicHeight(int picHeight) {
    		this.picHeight = picHeight;
    	}
    	/**
    	 * 设置柱状图坐标。数组长度需统一
    	 * @param x 柱状图X轴数据
    	 * @param y 柱状图Y轴数据
    	 */
    	public  void setHistogram(double[] x,double[] y) {
    		if(x==null||y==null||x.length==0||y.length==0){
    			System.out.println("数组不能为空");
    		}
    		else if(x.length==y.length){
    			this.histogramX=x;
    			this.histogramY=y;
    			hisArrLenth=x.length;
    		} else {
    			System.out.println("ERROR:柱状图数组长度不一致");
    		}
    	}
    	
    	/**
    	 * 设置折线图坐标。数组长度需统一
    	 * @param x
    	 * @param y
    	 */
    	public  void setPolyLine(double[] x,double[] y) {
    		if(x==null||y==null||x.length==0||y.length==0){
    			System.out.println("数组不能为空");
    		}
    		else if(x.length==y.length){
    			this.polylineX=x;
    			this.polylineY=y;
    			polyArrLenth=x.length;
    		} else {
    			System.out.println("ERROR:折线图数组长度不一致");
    		}
    	}
    	
    	public File getFile() {
    		return file;
    	}
    	/**
    	 * 设置生成图片位置默认为C盘
    	 * @param file
    	 */
    	public void setFile(File file) {
    		this.file = file;
    	}
    	public AxisImage(){
    		init();
    	}
    	
    	
    	/**
    	 * 计算参数
    	 */
    	private void init(){
    		axisw = picWidth * 3 / 4;// 坐标轴的宽度
    		axish = picHeight * 3 / 4;// 坐标轴的高度
    		axisx = (picWidth - axisw) / 2;// 坐标轴距离图片左侧边缘的距离
    		axisy = (picHeight - axish) / 2;// 坐标轴距离图片上方边距的距离
    		promptw = axisw * 1 / 4; // 提示框宽度
    		prompth = axish * 1 / 4; // 提示框高度
    		promptx1 = axisw - promptw + axisx;// 提示框左上起点坐标
    		prompty1 = axisy;
    		promptx2 = axisw + axisx;// 提示框右下起点坐标
    		prompty2 = axisy + prompth;
    		promptx3 = promptx1;// 提示框右下起点坐标
    		prompty3 = prompty2;// 提示框终点坐标
    		tipsLenth = promptw * 1 / 5;// 提示线长度
    		tipsx1 = promptx1 + 20;// 第一条提示内容起点坐标
    		tipsy1 = axisy + (prompth * 1 / 3);
    		tipsx1e = tipsx1 + tipsLenth;// 终点坐标
    		tipsy1e = tipsy1;// 终点坐标
    		tipsx2 = promptx1 + 20;// 第二条提示内容起点坐标
    		tipsy2 = axisy + (prompth * 2 / 3);
    		tipsx2e = tipsx2 + tipsLenth;// 终点坐标
    		tipsy2e = tipsy2;// 终点坐标
    		axisDownSub = 15;//下标长度
    		 bi = new BufferedImage(picWidth, picHeight, BufferedImage.TYPE_INT_RGB);
    		 g2 = (Graphics2D) bi.getGraphics();
    		 file = new File("c://image.jpg");
    	}
    	
    	
    	
    	public static void main(String[] args) throws IOException {
    
    		double x[] = { 66.51735, 67.99290, 68.03937, 68.17945, 68.26451, 68.53912, 68.78240, 69.76701, 71.48474,
    				71.49349, 71.61041, 71.80097, 71.85926, 72.26497, 72.42447, 72.52136, 73.01164, 73.42670, 73.87192,
    				73.95811, 74.04925, 74.44888, 74.46081, 74.57221, 74.70525, 74.79978, 74.87349, 75.10031, 75.26910,
    				75.27314, 75.34062, 75.60157, 75.68488, 75.72644, 75.97427, 76.10981, 76.25556, 76.44609, 76.72893,
    				76.76861, 76.92616, 76.95070, 77.10785, 77.33795, 77.70276, 77.71285, 78.06742, 78.12773, 78.13893,
    				78.31453, 78.37094, 78.62340, 79.46324, 79.76679, 80.01308, 80.04429, 80.19654, 80.40770, 80.65242,
    				80.94278, 81.38741, 81.50586, 82.04338, 82.17103, 82.31215, 82.48748, 82.50726, 82.53808, 83.92527,
    				83.93192, 84.09460, 84.35794, 84.94915, 85.15228, 85.83253, 85.93202, 86.06689, 86.07133, 86.15034,
    				86.73605, 86.79122, 86.93343, 87.44626, 88.01052, 89.37358, 90.56689, 91.60573, 92.89265, 93.67812,
    				94.66197, 96.01299, 100.86324, 104.06124, 112.14891, 115.12634, 123.47084, 131.75722, 133.32378,
    				133.39636, 187.60977 };
    		double y[] = { 0.01620721750518043, 0.018680329777074397, 0.018757638205890912, 0.018990271553027484,
    				0.01913121111031898, 0.01958434865155712, 0.019983058747101604, 0.02156255872113014,
    				0.024128960855586353, 0.024141252933342346, 0.02430464074377313, 0.02456742977322896,
    				0.02464692706252103, 0.02518831054464121, 0.025395245215649984, 0.025519269301828407,
    				0.026126704961675146, 0.02661352924102587, 0.02710635027688293, 0.027198131078343398,
    				0.02729387983531682, 0.02769764901408549, 0.02770929494028185, 0.027816886742571607,
    				0.02794262706722092, 0.028030136105431844, 0.02809730717453481, 0.02829810745392632,
    				0.028441704061816384, 0.028445079639060542, 0.02850103550823336, 0.02870980906704646,
    				0.028773897704059578, 0.028805402405391603, 0.028986790702632947, 0.029081273150083575,
    				0.029179125850552868, 0.029301158634387113, 0.029469951690041973, 0.029492445444651072,
    				0.029578867319686467, 0.029591912293486577, 0.029672786702218047, 0.02978287749886177,
    				0.02993709727995609, 0.02994100816055078, 0.030066319126732816, 0.030085289342203374,
    				0.03008873726537292, 0.03013972690683094, 0.030154883406218854, 0.03021543993545951,
    				0.03033188039105794, 0.030342167078195925, 0.030338287062276113, 0.030337019182637325,
    				0.030328345548057755, 0.030309518190870535, 0.030277909134270354, 0.03022694257160966,
    				0.030121091336621424, 0.03008731468578369, 0.02990526455644281, 0.02985523208135455,
    				0.029796944746510824, 0.029720227051724928, 0.029711275726741725, 0.029697209323152098,
    				0.028920436430107447, 0.028916066986058735, 0.028807362722478934, 0.02862413248839518,
    				0.028181385466879177, 0.028019687985944557, 0.0274448880626539, 0.02735672782247402,
    				0.027235627664920256, 0.027231610286206336, 0.027159797643435703, 0.026609071328126666,
    				0.02655559800097064, 0.0264165527117408, 0.02590132284881471, 0.025311455104784893,
    				0.023804498184610536, 0.02241471348992983, 0.02117176565702193, 0.01961307025895549,
    				0.018661784512994606, 0.017480030320616933, 0.01589059411282775, 0.010751618100699533,
    				0.008005222273991584, 0.003396227516509402, 0.00239269738884818, 8.267933333062738E-4,
    				2.61225876013346E-4, 2.0821474350606197E-4, 2.060252759721704E-4, 3.0214472993361264E-8 };
    		
    		double[] x1={57.087821906189085,61.86829668646384,65.44380903384717,66.89505223441292,67.08262528953466,67.1537114146997,67.55315528461333,68.30973099005645,69.86700058629107,69.91673288061753,70.00274426101426,70.29061741840201,70.54500424784342,70.62502824319151,71.09693232201074,71.17086262984385,71.17184289825569,71.50389356048164,71.7883330374566,71.81662714897821,72.07165753339419,72.8010062763422,73.06803645948806,73.9553153159744,74.05239914483197,74.40011861776259,74.48012780417062,74.58653159081715,74.62916270435953,75.18087783954779,75.56948598143467,76.14336856997399,76.26080748169719,77.04570620520252,77.14372033563772,78.00547601093457,78.48112260210435,79.5144595853992,80.02146069670127,80.80392076518011,81.12556223401566,82.28105400309575,82.45719693490885,83.36673051114917,83.83036245165579,84.03129754681467,84.08389010773504,84.2221344083095,84.35916164637327,84.93422330121743,85.0228962615539,85.03463038255056,85.22953150054086,85.35541332572895,85.65496983667116,85.67242793079159,85.71581705917895,86.00629325352,86.59174806237682,86.69363897621801,86.72225859684566,87.49775465201179,88.29474541884464,88.3220845781021,88.68917406844959,88.96120457864463,89.77756883559795,90.68308726679562,91.22687565491556,91.40018900974428,92.01543585538741,92.26523955418429,92.85891424375406,93.27214052310934,93.68382441712944,93.88157704356753,94.24242681436122,95.045279786781,95.17723406266161,96.35113967243367,96.52082262718706,97.03255548640439,97.21339218240152,98.12407358448557,99.15128548333513,99.56785816214983,99.90283094065677,100.13398881292308,101.09622664008035,101.26250041310169,102.75031441559763,103.53413791857635,104.16057027365471,105.14985995835553,105.23545051447715,107.20105424583014,107.92747435137335,108.79388499463151,110.07023239755166,119.10335275481114};
    		double[] y1={0.0036347217615213608,0.008909546362932267,0.014416607562305346,0.016841559837040113,0.01715667957072753,0.017276068365196493,0.01794603137069616,0.019206033837249736,0.02171927321049496,0.0217969289235467,0.02193076827749751,0.02237423935171944,0.022760028331417336,0.02288013807324589,0.02357542781366693,0.02368224202198765,0.023683654311760664,0.02415585632829236,0.024550139833098894,0.024588824652643882,0.024932993090543955,0.025869929040009815,0.026194360125204548,0.027195173855162562,0.027297164159998503,0.027649800841545266,0.027728102094468934,0.02783056681603052,0.027871082640665262,0.02836727402460908,0.028684795419925316,0.029104148209240965,0.029182576235673365,0.029641356555713518,0.02969060025147046,0.03004612575127828,0.03018277403074368,0.030334789893238744,0.030337963691998808,0.030253126448055678,0.030187462825876088,0.02981005512801749,0.029733815534711914,0.029266070274262212,0.028982155233572497,0.02885007455030215,0.028814625689692324,0.02871973374104428,0.028623261901084246,0.028193079338534387,0.028123230775711737,0.028113919423036284,0.027956958051184935,0.027853308530825262,0.02759969975050787,0.027584623960533995,0.0275470172546612,0.027290261769822178,0.026747668958051255,0.026649997124195256,0.02662239708985035,0.02584844850336921,0.025006106861200156,0.024976465507401093,0.02457412856098657,0.02427107793319393,0.023339937097254054,0.02227688405935947,0.021627540475739043,0.021419325952077437,0.020676700598799162,0.020374107394217827,0.01965399550424387,0.019152973294984194,0.018654893685926342,0.018416241030844133,0.01798209625108659,0.017024412985902345,0.01686834326976188,0.015500936373598154,0.015306836807832264,0.014727649184102347,0.014525308269999026,0.013526369568220509,0.012443196318062746,0.012018089137311434,0.011682438034222066,0.011454089475752625,0.0105330171378685,0.010378758790826378,0.009064251166119966,0.008419844585697446,0.007928769924351686,0.007196244824320588,0.007135322208980008,0.005841145154551744,0.005412258665539971,0.004933988963128097,0.004292422768586651,0.0014620243145961713};
    		
    		AxisImage ai = new AxisImage();
    		ai.setHistogram(x, y);
    		ai.setPolyLine(x1, y1);
    		ai.createAxisImagePanel();
    		
    		
    //		createAxisImagePanel(x, y,x1,y1);
    	}
    
    	/**
    	 * 生成图片坐标系模板
    	 * 
    	 * @param x
    	 *            x轴数据
    	 * @param x
    	 *            y轴数据
    	 * @return
    	 */
    	private  boolean createAxisImagePanel() {
    
    	
    
    		try {
    			g2.setBackground(Color.WHITE); // 设置背景颜色
    			g2.fillRect(0, 0, picWidth, picHeight);
    
    			// 设置坐标轴外部边框
    			g2.setColor(Color.black);
    			Rectangle2D rim = new Rectangle2D.Double(axisx, axisy, axisw, axish);// 创建矩形对象
    			g2.draw(rim);
    
    			// 设置坐标轴右上提示窗口
    
    			Line2D promptx = new Line2D.Double(promptx1, prompty1, promptx3, prompty3);// 创建line对象
    			g2.draw(promptx);// 提示框竖线
    			Line2D prompty = new Line2D.Double(promptx2, prompty2, promptx3, prompty3);// 创建line对象
    			g2.draw(prompty);// 提示框横线
    
    			// 提示框内容
    			Line2D tip1 = new Line2D.Double(tipsx1, tipsy1, tipsx1e, tipsy1e);// 创建line对象
    			g2.draw(tip1);// 提示框竖线
    			g2.drawString("True Density", tipsx1e + 20, tipsy1);
    
    			// 提示框内容
    			Line2D tip2 = new Line2D.Double(tipsx2, tipsy2, tipsx2e, tipsy2e);// 创建line对象
    			g2.setColor(Color.red);
    			g2.draw(tip2);// 提示框竖线
    			g2.setColor(Color.black);
    			g2.drawString("Estimate", tipsx2e + 20, tipsy2);
    			
    			
    			
    			double maxAndIntervalX[] =null;
    			// 获取坐标系区间
    			if(hisFlag&&polyFlag){
    				if(PubUtil.getArrMaxValue(histogramX)>PubUtil.getArrMaxValue(polylineX)){
    					maxAndIntervalX=getAxis(histogramX);
    				}else {
    					maxAndIntervalX=getAxis(polylineX);
    				}
    			}else if (hisFlag){
    				maxAndIntervalX=getAxis(histogramX);
    			}else if (polyFlag){
    				maxAndIntervalX=getAxis(histogramX);
    			} else {
    				System.out.println("无生成图片数据");
    				return false;
    			}
    			
    			
    			double[][] xDownAxis = getDownAxis(maxAndIntervalX,"x");//获取坐标数组
    			
    			for (int i = 1; i < xDownAxis.length; i++) {
    				g2.draw(new Line2D.Double(xDownAxis[i][0],xDownAxis[i][1],xDownAxis[i][2],xDownAxis[i][3]));
    				double downAxis=maxAndIntervalX[1] * i;
    				String da=downAxis%1==0?new DecimalFormat("0").format(downAxis):String.valueOf(downAxis);
    				g2.drawString(da,(int) (xDownAxis[i][0] - da.length()*3),(int) (xDownAxis[i][3] + 15));
    			}
    			
    			
    			
    			double maxAndIntervalY[] =null;
    			// 获取坐标系区间
    			if(hisFlag&&polyFlag){
    				if(PubUtil.getArrMaxValue(histogramY)>PubUtil.getArrMaxValue(polylineY)){
    					maxAndIntervalY=getAxis(histogramY);
    				}else {
    					maxAndIntervalY=getAxis(polylineY);
    				}
    			}else if (hisFlag){
    				maxAndIntervalY=getAxis(histogramY);
    			}else if (polyFlag){
    				maxAndIntervalY=getAxis(histogramY);
    			}
    			
    			double[][] yDownAxis = getDownAxis(maxAndIntervalY,"y"); //获取坐标数组
    	        AffineTransform atf = (AffineTransform) g2.getTransform().clone();//保存初始坐标系,在旋转后还原
    
    			for (int i = 1; i < yDownAxis.length; i++) {
    				g2.draw(new Line2D.Double(yDownAxis[i][0],yDownAxis[i][1],yDownAxis[i][2],yDownAxis[i][3]));
    				double downAxis=maxAndIntervalY[1] * i;
    				String da=downAxis%1==0?new DecimalFormat("0").format(downAxis):String.valueOf(downAxis);
    				g2.translate(yDownAxis[i][2]-15, yDownAxis[i][3]-da.length()*3);
    				g2.rotate(Math.PI*90/180);//旋转
    				g2.drawString(da,0,0);
    				g2.setTransform(atf);
    			}
    			if(hisFlag){
    				createHistogram(histogramX,histogramY);
    			}
    			if(polyFlag){
    				createPolyline(polylineX, polylineY);
    			}
    		
    			ImageIO.write(bi, "jpg", file);
    			System.out.println("生成图片");
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			return false;
    		}
    
    		return true;
    	}
    	
    	/**
    	 * 生成柱状图
    	 * @param x
    	 * @param y
    	 * @return
    	 */
    	private boolean createHistogram(double x[],double y[]){
    		//生成柱状图
    		try {
    			for (int i = 0; i < hisArrLenth; i++) {
    				double []axis=getHistogramLocation(x[i], y[i]);
    				Line2D histogram = new Line2D.Double(axis[0], axis[1], axis[2], axis[3]);// 创建line对象
    				g2.draw(histogram);
    			}
    		} catch (Exception e) {
    			return false;
    		}
    		return true;
    	}
    	
    	private boolean createPolyline(double x[],double y[]){
    		try {
    			g2.setColor(Color.RED);
    			double [][]polyline = getPolylineLocation(x, y);
    			for (int i = 0; i < polyline.length; i++) {
    				Line2D histogram = new Line2D.Double(polyline[i][0], polyline[i][1], polyline[i][2], polyline[i][3]);// 创建line对象
    				g2.draw(histogram);
    			}
    		} catch (Exception e) {
    			return false;
    			// TODO: handle exception
    		}
    		
    		
    		return true;
    	}
    	
    	/**
    	 * 获取坐标系 
    	 * @param axisFlag x轴或y轴
    	 * @return
    	 */
    	private  double[][] getDownAxis(double[] axis,String axisFlag){
    		double[][] result=new double[5][4];//返回结果
    		if("x".equalsIgnoreCase(axisFlag)){
    			 oneElementWidthX = axisw / axis[0];// 每个坐标轴精度占据的长度
    			for (int i = 0; i < result.length; i++) {
    				for (int j = 0; j < result[i].length; j=j+2) {
    					result[i][j]=i * axis[1] * oneElementWidthX+axisx;
    				}
    				for (int j = 1; j < result[i].length; j=j+2) {
    					result[i][j]= axish + axisy;
    				}
    				result[i][3]+=axisDownSub;
    			}
    		} else if ("y".equalsIgnoreCase(axisFlag)){
    			 oneElementWidthY = axish / axis[0];// 每个坐标轴精度占据的长度
    			for (int i = 0; i < result.length; i++) {
    				for (int j = 0; j < result[i].length; j=j+2) {
    					result[i][j]= axisx;
    				}
    				for (int j = 1; j < result[i].length; j=j+2) {
    					result[i][j]= axisy+axish-(i * axis[1] * oneElementWidthY);
    				}
    				result[i][2]-=axisDownSub;
    			}
    		}
    		
    		
    		return result;
    	}
    	/**
    	 * 生成柱状图坐标系
    	 * @param xdata x轴坐标
    	 * @param ydata y轴坐标
    	 * @return
    	 */
    	private  double[] getHistogramLocation(double xdata,double ydata){
    		double[] result = new double[4];
    		result[0] = xdata*oneElementWidthX+axisx;
    		result[1] = axisy+axish;
    		result[2] = result[0];
    		result[3] = result[1]-(ydata*oneElementWidthY);
    		return result;
    	}
    	
    	/**
    	 * 生成折线图坐标系
    	 * @param xdata x轴坐标
    	 * @param ydata y轴坐标
    	 * @return
    	 */
    	private  double[][] getPolylineLocation(double xdata[],double ydata[]){
    		double result[][] = new double[polyArrLenth-1][4];
    		
    		for (int i = 0; i < result.length; i++) {
    			 result[i][0]=xdata[i]*oneElementWidthX+axisx;
    			 result[i][1]=axisy+axish-(ydata[i]*oneElementWidthY);
    			 result[i][2]=xdata[i+1]*oneElementWidthX+axisx;;
    			 result[i][3]=axisy+axish-(ydata[i+1]*oneElementWidthY);
    		}
    		
    		return result;
    	}
    	
    	private static double[] getAxis(double[] x) {
    		double max = PubUtil.getArrMaxValue(x);
    		double interval = 0;
    		int bit = 0;
    		if (max > 100) {
    			do {
    				bit++;
    				max /= 10;
    			} while (max > 100);
    		} else if (max < 10) {
    			do {
    				bit--;
    				max *= 10;
    			} while (max < 10);
    		}
    		interval = Math.ceil(max / 5);
    		max = interval * 5;
    		return new double[] { max * Math.pow(10, bit), interval * Math.pow(10, bit) };
    	}
    
    }
    


    展开全文
  • 对数正态lognormal分布图像

    万次阅读 2015-12-15 19:16:04
    在概率论与统计学中,对数正态分布对数为正态分布的任意随机变量的概率分布。如果 X 是服从正态分布的随机变量,则 exp(X) 服从对数正态分布;同样,如果 Y 服从对数正态分布,则 ln(Y) 服从正态分布。 如果一...
    在概率论与统计学中,对数正态分布是对数为正态分布的任意随机变量的概率分布。如果 X 是服从正态分布的随机变量,则 exp(X) 服从对数正态分布;同样,如果 Y 服从对数正态分布,则 ln(Y) 服从正态分布。 如果一个变量可以看作是许多很小独立因子的乘积,则这个变量可以看作是对数正态分布。一个典型的例子是股票投资的长期收益率,它可以看作是每天收益率的乘积。
    设ξ服从对数正态分布,其密度函数为:
    对数正态分布的密度函数
    clc,clear,close all
    warning off
    feature jit off
    im = imread('coloredChips.png');
    Z1 = imnoise_lognormal(size(im,1),size(im,2),2,3);
    Z1 = uint8(Z1);   % 类型转换
    figure('color',[1,1,1]),
    im(:,:,1) = im(:,:,1) + Z1;  % R
    im(:,:,2) = im(:,:,2) + Z1;  % G
    im(:,:,3) = im(:,:,3) + Z1;  % B
    subplot(121); imshow(im);title('加对数正态分布噪声图像')
    subplot(122); imhist(Z1); title('加对数正态分布噪声图像直方图')
    

    function R = imnoise_lognormal(M, N, a,b)
    % input:
    %       对数正态lognormal分布,噪声的类型;
    %       M,N:输出噪声图像矩阵的大小
    %       a,b:各种噪声的分布参数
    % output:
    %       R: 输出的噪声图像矩阵,数据类型为double型
    % 设定默认值
       % 产生对数正态分布噪声
       if nargin <= 3
          a = 1; b = 0.25;
       end
       x = log(randn(M, N));
       R = a*exp(b*x);
    end


    展开全文
  • 为了更准确地拟合图像的目标与背景的灰度级分布并分割出图像的目标部分,采用基于参数的阈值估计方法,提出了基于对数正态分布的粒子群EM混合算法,设计了对数正态分布参数的粒子群算法、EM算法和粒子群EM混合算法,给出...
  • 对数正态分布(logarithmic normal distribution)是指一个随机变量的对数服从正态分布,则该随机变量服从对数正态分布对数正态分布从短期来看,与正态分布非常接近。但长期来看,对数正态分布向上分布的数值更...

    “对数正态分布(logarithmic normal distribution)是指一个随机变量的对数服从正态分布,则该随机变量服从对数正态分布。对数正态分布从短期来看,与正态分布非常接近。但长期来看,对数正态分布向上分布的数值更多一些。”






    展开全文
  • 1,所以你应该能够在变换的上有负值. 这是从matplotlib文档中修改的example,显示了如何将自定义转换结合到“scale”中:import numpy as npfrom numpy import mafrom matplotlib import scale as mscalefrom ...

    实际上,您需要将以下转换应用于Y值:-log10(1-y).这仅限制了y < 1,所以你应该能够在变换的图上有负值. 这是从matplotlib文档中修改的

    example,显示了如何将自定义转换结合到“scale”中:

    import numpy as np

    from numpy import ma

    from matplotlib import scale as mscale

    from matplotlib import transforms as mtransforms

    from matplotlib.ticker import FixedFormatter, FixedLocator

    class CloseToOne(mscale.ScaleBase):

    name = 'close_to_one'

    def __init__(self, axis, **kwargs):

    mscale.ScaleBase.__init__(self)

    self.nines = kwargs.get('nines', 5)

    def get_transform(self):

    return self.Transform(self.nines)

    def set_default_locators_and_formatters(self, axis):

    axis.set_major_locator(FixedLocator(

    np.array([1-10**(-k) for k in range(1+self.nines)])))

    axis.set_major_formatter(FixedFormatter(

    [str(1-10**(-k)) for k in range(1+self.nines)]))

    def limit_range_for_scale(self, vmin, vmax, minpos):

    return vmin, min(1 - 10**(-self.nines), vmax)

    class Transform(mtransforms.Transform):

    input_dims = 1

    output_dims = 1

    is_separable = True

    def __init__(self, nines):

    mtransforms.Transform.__init__(self)

    self.nines = nines

    def transform_non_affine(self, a):

    masked = ma.masked_where(a > 1-10**(-1-self.nines), a)

    if masked.mask.any():

    return -ma.log10(1-a)

    else:

    return -np.log10(1-a)

    def inverted(self):

    return CloseToOne.InvertedTransform(self.nines)

    class InvertedTransform(mtransforms.Transform):

    input_dims = 1

    output_dims = 1

    is_separable = True

    def __init__(self, nines):

    mtransforms.Transform.__init__(self)

    self.nines = nines

    def transform_non_affine(self, a):

    return 1. - 10**(-a)

    def inverted(self):

    return CloseToOne.Transform(self.nines)

    mscale.register_scale(CloseToOne)

    if __name__ == '__main__':

    import pylab

    pylab.figure(figsize=(20, 9))

    t = np.arange(-0.5, 1, 0.00001)

    pylab.subplot(121)

    pylab.plot(t)

    pylab.subplot(122)

    pylab.plot(t)

    pylab.yscale('close_to_one')

    pylab.grid(True)

    pylab.show()

    请注意,您可以通过关键字参数控制9的数量:

    pylab.figure()

    pylab.plot(t)

    pylab.yscale('close_to_one', nines=3)

    pylab.grid(True)

    展开全文
  • 1给出了正态分布(对称分布)和对数正态分布(偏态分布)的拟合实例。注意,身高符合这两种分布1正态分布对数正态分布的例子。a,1052名女性的身高分布拟合正态分布,拟合优度p值0.75, b,1573份蜂蜜样品中羟甲...
  • 要求:生成的目标对数正态分布随机数要介于区间[log_lower,log_upper]内,这里设定该区间为[5, 10],并绘制正态分布对数正态分布随机数的直方。源代码:import numpy as npfrom pylab import...
  • stats.probplot(grade, dist=stats.norm, plot=plt) #正态分布 # stats.probplot(grade, dist=stats.expon, plot=plt) #指数分布 # stats.probplot(grade, dist=stats.logistic, plot=plt...
  • 为了从观察图像中估计出合成孔径雷达(SAR)图像的雷达横截面积(RCS)模型的对数—正态分布的参数, 提出了强度图像和幅值图像的参数估计方法。推导出斑点的前两阶对数累积量和对数—正态分布的前两阶对数累积量; ...
  • 一、正态分布的特征:1为英国生物统计学家高尔顿专门设计用来演示正态分布曲线及其特征的实验模型,通常称为高尔顿板(Galton Board)。高尔顿板上的每一个圆点表示钉在板上的钉子,钉子之间的距离彼此相等,呈...
  • %from this on ...%对数正态分布密度函数 x=(0:0.02:6)*1e4; y=fun([3e5,8.3,0.6],x)+rand(size(x)).*exp(-(x/2e4).^2)*20; %原来你的可能是用hist(data,n)画出来的 %就是将data数据分n个...
  • 对于大小为n的随机样本,间隔中的预期样本数为(cdf(b) - cdf(a))*n其中cdf(x)是累积分布函数 . 要绘制预期的直方,您将计算每个bin的值 .下面的脚本显示了在matplotlib直方上绘制预期直方的一种方法 . 它生成...
  • 为了研究煤矿井下疏放水钻孔水量的分布规律,利用直方、Q-Q和非参数检验法对典型工作面疏放水钻孔水量进行了对数正态分布模型的检验,并进行了非线性回归模型的拟合,结合工作面水文地质条件,对模型中的参数...
  • 要求:生成的目标对数正态分布随机数要介于区间[log_lower,log_upper]内,这里设定该区间为[5, 10],并绘制正态分布对数正态分布随机数的直方。 源代码: import numpy as np from py...
  • 对数正态分布 关于对数正态分布的几个关键点: 当变化是由加和在一起的多个散点来源引起时,就会出现高斯分布。... 在对数轴上绘图时,如上右侧所示,分布是对称分布; 如果你不知道对数正..
  • 采用root 作对数坐标,通常会出现横坐标不是均等宽度分布,这是由于在定义historgram,在设置bin的个数和上下限时默认时均有分割,而不是按对数宽度均匀分割。为此,需要自定义bin的分割位置。 具体代码如下 //--...
  • 散点、残差、对数图

    千次阅读 2018-07-17 20:44:01
    散点 噪声:平滑(样条、LOSSE局部线性回归) 残差(均匀分布、不应该具有某种趋势、有正有负) 对数图(控制数据中的幅度、乘法转换加法、指数及幂律行为)...
  • 我们描述了具有重尾瑞利分布的多视极化SAR图像的成功建模,并提出了基于矩阵对数累积量的重尾瑞利分布包括等效视数(ENL)的新颖参数估计器。 首先,将重尾瑞利分布的复合变量分为正α稳定变量和复数高斯变量的乘积...
  • 正态分布:球员身高NFL球员的身高数据分布形态非常类似于高斯分布,也就是正态分布,如下所示。正态曲线是在统计学界被誉为是神一样的存在,是诸多在统计学意义上进行比较的基础,既是一种超级强...
  • 拉丁超立方抽样-对数正态分布0、拉丁超立方抽样的理论基础1、导入库和基本准备2、生成(具有对数正态分布的随机变量)参数的随机数3、将生成的随机数输出到Excel中4、将生成的随机数输出到图像中5、代码肯定可以实现...
  • 在学习生活中,我们经常性的发现有很多事物背后都有某种规律,而且,这种规律可能符合某种随机分布,比如:正态分布对数正态分布、beta分布等等。 所以,了解某种分布对一些事物有更加深入的理解并能清楚的阐释...
  • 在学习生活中,我们经常性的发现有很多事物背后都有某种规律,而且,这种规律可能符合某种随机分布,比如:正态分布对数正态分布、beta分布等等。所以,了解某种分布对一些事物有更加深入的理解并能清楚的阐释事物...
  • 自己编写的matlab程序,用于生成BA网络并画度分布的双对数图
  • 在学习生活中,我们经常性的发现有很多事物背后都有某种规律,而且,这种规律可能符合某种随机分布,比如:正态分布对数正态分布、beta分布等等。所以,了解某种分布对一些事物有更加深入的理解并能清楚的阐释事物...
  • 用python生成各种随机分布图

    万次阅读 2017-06-06 23:37:08
    在学习生活中,我们经常性的发现有很多事物背后都有某种规律,而且,这种规律可能符合某种随机分布,比如:正态分布对数正态分布、beta分布等等。 所以,了解某种分布对一些事物有更加深入的理解并能清楚的阐释...
  • 如何使用Q-Q验证数据的分布

    千次阅读 2020-09-05 12:03:11
    Q-Q是检验任何随机变量(如正态分布、指数分布对数正态分布等)分布的图形方法,是观察任何分布性质的一种统计方法。 例如,如果给定的一个分布需要验证它是否是正态分布,我们运行统计分析并将未知分布与已知...
  • 这篇简单的文章主要是针对我们老师...利用图像直方对对比度进行调整的方法,使得变换后的图像直方均匀分布,增加了图像的全局对比度,图像也就越清晰,色彩也就越丰富。 2.1.2 实验步骤 n 统计灰度直方 ...
  • 然后给出了不同分布的高阶矩和对数累积量,通过二三阶对数累积量关系辨析了常用分布与L分布的内在关系,得到了L分布是目前乘积模型中适用范围较为广泛的统计分布的结论.最后用仿真数据验证了理论推导的正确性,并将...
  • 在学习生活中,我们经常性的发现有很多事物背后都有某种规律,而且,这种规律可能符合某种随机分布,比如:正态分布对数正态分布、beta分布等等。所以,了解某种分布对一些事物有更加深入的理解并能清楚的阐释事物...
  • 鸽了好几天,一直没想好更新什么,翻了下以前画过的一些图,突然想到纬高图也是气象研究中心很常见的一类图型,通常用于...1979年9月北半球温度的纬度-高度分布图我准备的数据是ERA-Interim再分析资料的1979年9月份...

空空如也

空空如也

1 2 3 4 5 ... 19
收藏数 374
精华内容 149
关键字:

对数分布图