python - matplotlib pyplot 2 plots with different axes in same figure -
I have a small issue with matplotlib.pyplot and I hope there may be anyone before this
I have data that has x, y, e values, which are x, measure of variables and errors in measurement in e. I need to plot them in log log scale.
I use the plt.errorbars function to plot them and then I set yscale and xscale to log in And it works fine. But I need a plot of the line on the same graph that should be on linear scale.
I am able to fix the plots separately, but if possible, then I want to put them in the same image. Do you have any ideas? I am posting what I have done for 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 data = Reader ( Filename) data2 = logconv (data) #x, y, e data useful calculation of calculation x = data 2 [0] y = data 2 [1] e = data 2 [2] xoe2 = np. (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) Slop = (xoe2 * yoe2-xyoe2 * OE2) / (xoe2 ** 2-x2oe2 * OE2) binter = (in xyoe2- shield case * x2oe2) / xoe2 aerr = np.sqrt (OE2 / (x2oe2 * OE2-xoe2 ** 2)) berr = np.sqrt (x2oe2 / (X2oe2 * oe2-xoe2 ** 2)) print ( 'downhill', Slop, '+ - ', Error) print (' inter 'is, tooth,' + - ', hairs) fig = plt.figure () Ax1 = fig.add_subplot (1,1,1) ax2 = fig.add_axes (ax1.get_position () , Framing = false) ax1.errorbar (data [0], data [1], yerr = data [2], fmt = 'o') ax1.set_xscale ('logon', basex = 10) ax1.set_yscale (' Logon ', basey = 10) ax1.set_yticks ([]) ax1.set_xticks ([]) ax2.plot (x, aslope * x + binator,' r ') ax2.plot (x, (aslope-aerr) * X + (Bainr + 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 (tdli St.), tdlist.astype ( 'name')) plt.xlabel ( 'frequency (MHz ) ') Plt.ylabel (' T_S (msec) ') fitndx1 =' Fit slope '+ "{0: .2f}". Format (Aslope) + U "\ u00B1" + "{0: .2f}". Format (ARR) PLT.Legend (('Data', Fitand x1)) plt.show ()
After Molly's suggestion, I managed to get close to my goal but still Not there. For what I am trying to do, I am adding a bit more information and it can clarify some things.
I am setting Arrow 1 for aerobor plot which uses the logogram scale. I do not have to use the error bar and there is no log-in plot, so that I can display the errors with my digits.
I am using ax2 to plot linear fit in linear scale.
In addition to this, I do not want to display the powers of x and y axis to 10,100,1000 ten, but my own horoscope labeled with spacing, so I want to plt.xticks I am using I tried ax1.set_yticks and ax1.set_yticklabes but with no success. Below is the image that I am getting.
I do not have enough reputation to post an image but here's the upload link
My points are X range = 40 - 80 and y range = 5-200 should be because the fit lines are now.
You can create two overlapping axes using the add_suplot method. Here is an example:
plt figs as matplotlib import = plt.figure (from pyplot) AX1 = fig.add_subplot (1,1,1) AX2 = fig.add_axes (ax1. Get_position (), framing = falls) ax1.loglog ([1,10,100,1000], [1000,1,100,10]) ax2.plot ([5,10,11,13], 'r') plt.show ( )
Then you can close X and Y ticks for plotting a linear scale like this:
ax2.set_xticks ([]) ax2.set_yticks ([])
Comments
Post a Comment