Sunday 15 April 2012

python - matplotlib pyplot 2 plots with different axes in same figure -



python - matplotlib pyplot 2 plots with different axes in same figure -

i have little issue matplotlib.pyplot , hope might have come across before.

i have info contain x,y,e values x, y measurements of variable , e errors of measurements in y. need plot them in log log scale.

i utilize plt.errorbars function plot them , set yscale , xscale log , works fine. need plot line on same graph needs in linear scale.

i able have plots done separately fine have them in same image if possible. have ideas? posting have done now.

cheers, kimon

tdlist = np.array([0.01,0.02,0.05,0.1,0.2,0.3,0.4,0.5,0.8,1,2,5,10,15,20,25,30,40,60,80,100,150,200,250,300,400]) freqlist=np.array([30,40,50,60,70,80,90,100,110,120,140,160,180,200,220,250,300,350,400,450]) filename=opts.filename info = reader(filename) data2 = logconv(data) #x,y,e data. calculating usefull sums x = data2[0] y = data2[1] e = data2[2] xoe2 = np.sum(x/e**2) yoe2 = np.sum(y/e**2) xyoe2 = np.sum(x*y/e**2) oe2 = np.sum(1/e**2) x2oe2 = np.sum(x**2/e**2) aslope = (xoe2*yoe2-xyoe2*oe2)/(xoe2**2-x2oe2*oe2) binter = (xyoe2-aslope*x2oe2)/xoe2 aerr = np.sqrt(oe2/(x2oe2*oe2-xoe2**2)) berr = np.sqrt(x2oe2/(x2oe2*oe2-xoe2**2)) print('slope ',aslope,' +- ', aerr) print('inter ',binter,' +- ', berr) fig = plt.figure() ax1 = fig.add_subplot(1,1,1) ax2 = fig.add_axes(ax1.get_position(), frameon=false) ax1.errorbar(data[0],data[1],yerr=data[2],fmt='o') ax1.set_xscale('log',basex=10) ax1.set_yscale('log',basey=10) ax1.set_yticks([]) ax1.set_xticks([]) ax2.plot(x,aslope*x+binter,'r') ax2.plot(x,(aslope-aerr)*x+(binter+berr),'--') ax2.plot(x,(aslope+aerr)*x+(binter-berr),'--') ax2.set_xscale('linear') ax2.set_yscale('linear') plt.xticks(np.log10(freqlist),freqlist.astype('int')) plt.yticks(np.log10(tdlist),tdlist.astype('float')) plt.xlabel('frequency (mhz)') plt.ylabel('t_s (msec)') fitndx1 = 'fit slope '+"{0:.2f}".format(aslope)+u"\u00b1"+"{0:.2f}".format(aerr) plt.legend(('data',fitndx1)) plt.show()

following molly's suggestion managed closer goal still not there. adding bit more info trying , might clarify things bit.

i setting ax1 errobar plot uses loglog scale. need utilize errorbar , not loglog plot can display errors points.

i using ax2 plot linear fit in linealinear scale.

moreover not want x , y axes display values 10,100,1000 powers of 10 own axes labels have spacing want hence using plt.xticks. tried ax1.set_yticks , ax1.set_yticklabes no success. below image getting.

i not have plenty reputation post image here link of uploaded

http://postimg.org/image/uojanigab/

the values of points should x range = 40 - 80 , y range = 5 -200 fit lines now.

you can create 2 overlapping axes using add_suplot method of figure. here's example:

from matplotlib import pyplot plt fig = plt.figure() ax1 = fig.add_subplot(1,1,1) ax2 = fig.add_axes(ax1.get_position(), frameon=false) ax1.loglog([1,10,100,1000],[1000,1,100,10]) ax2.plot([5,10,11,13],'r') plt.show()

you can turn off x , y ticks linear scale plot this:

ax2.set_xticks([]) ax2.set_yticks([])

python matplotlib plot axes

No comments:

Post a Comment