Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a pdf image inside a subplot in matplotlib

I want a pdf(preferably) image inside a subplot in matplotlib to show the plots at different stages of the cycle. I tried with imshow but could not get it inside the subplot. Is there a method to do this? At the moment, I had to import the pdf of subplots to inkscape and edit the vector graphics to add these curve positions!! and its difficult to create the right alignment!! Would appreciate any suggestions.

ax2 = f.add_subplot(182, sharex=ax1)
ax2.plot(P1_05[:,1], P1_05[:,0], 'k-')
im = plt.imread('./1_cycle.png') #I want to add a pdf if possible!
implot = plt.imshow(im, extent=[0.01,0.8,1.2,2.0])
xlim(0,1.4)
ylim(0,2)

I want a pdf(preferably) image inside a subplot in matplotlib to show the plots at different stages of the cycle. I tried with <code>imshow</code> but could not get it inside the subplot. Is there a method to do this? At the moment, I had to import the pdf of subplots to inkscape and edit the vector graphics to add these curve positions!! and its difficult to create the right alignment!!Would appreciate any suggestions.

like image 797
Thangam Avatar asked Sep 07 '26 19:09

Thangam


2 Answers

Maybe as an idea to start with:

x1=np.linspace(0,np.pi)
y1=np.sin(x1)

y2=np.sin(x1)

rect1=[0.1,0.1,0.8,0.8]
ax1=plt.axes(rect,frameon=True)
ax1.yaxis.tick_left()
plt.plot(x1,y1)
plt.ylabel('axis 1')
plt.xlabel('x')


rect2=[0.1,1,0.2,0.2]
ax2=plt.axes(rect2,frameon=False)
ax2.yaxis.tick_right()
ax2.plot(x1,y2)

percent = 0.2
xp = percent*np.pi
yp = np.sin(xp)
ax2.plot(xp,yp, marker='o')

ax2.yaxis.set_label_position('right')
ax2.axes.get_xaxis().set_visible(False)
ax2.axes.get_yaxis().set_visible(False)

ax2.annotate('%d Percent' %(percent*100), xy=(0.5, 0.))


rect3=[0.3,1,0.2,0.2]
ax3=plt.axes(rect3,frameon=False)
ax3.yaxis.tick_right()
ax3.plot(x1,y2)

percent = 0.4
xp = percent*np.pi
yp = np.sin(xp)
ax3.plot(xp,yp, marker='o')

ax3.yaxis.set_label_position('right')
ax3.axes.get_xaxis().set_visible(False)
ax3.axes.get_yaxis().set_visible(False)

ax3.annotate('%d Percent' %(percent*100), xy=(0.5, 0.))




plt.show()

enter image description here

like image 110
Moritz Avatar answered Sep 10 '26 09:09

Moritz


so this is how I tweaked Moritz's code:

ax2 = f.add_subplot(182, sharex=ax1)
ax2.plot(P1_05[:,1], P1_05[:,0], 'k-')   
plt.tick_params(axis='both', which='major', labelsize=14)
xlim(0,1.4)
ylim(0,2)
#-------------------------nested plot for cycle position at the top-----------------
x1=np.linspace(0,2*np.pi) # length between 0 to 2pi
y1=np.sin(x1)
rect=[0.125,0.82,0.095,0.08] #x, y,width,height
plt.axes(rect)
plt.tick_params(bottom='off',labeltop='off',labelbottom='off', labelleft='off', left='off',top='off',right='off') #this turns off all the ticks for this plot
plot(x1,y1,'k-') # for the sine curve   
xlim(0,6.28) #limits for this plot(0,2pi)
ylim(-1.5,1.5) #limits are (-1,1) but .5 for space
percent = 0.2
xp = percent*np.pi
yp = np.sin(xp)
plot(xp,yp, marker='o') 
plt.annotate('%d \%%' %(percent*100), xy=(1.14, -1),fontsize=10)  #after much searching finally found this \%% for a percentage sign!
#-------------end of nested plot------------------

enter image description here

like image 22
Thangam Avatar answered Sep 10 '26 10:09

Thangam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!