3

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.

Thangam
  • 169
  • 3
  • 11
  • I do not really get what you want exactly. Which cycle ? What is your data ? How do you do it currently ? – Moritz May 25 '15 at 11:53
  • apologies if my question was not clear enough. By cycles I mean the small pdf at the top of each subplot indicating percentages. I can plot the subplots(my data) but these cycle indicators at the top are drawn pdfs and placed at the top using inkscape! I want to know if I can place this cycle indicating pdf with matplotlib inside the subplot on the top. – Thangam May 25 '15 at 12:13
  • please post your code – Moritz May 25 '15 at 12:15
  • posted the part of the code where I am trying to add the image into my subplot. – Thangam May 25 '15 at 12:23

2 Answers2

1

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

Moritz
  • 5,130
  • 10
  • 40
  • 81
  • I almost completed it and it works with some tweaks. Also, how to use the symbol % instead of the word 'Percent' in the last but line in your code? I tried converting to the raw string but escaping this symbol inside annotate is too tricky! any thoughts? I can post the code if thats sorted out too! otherwise I have to change it to raw strings every time for the % that I want. I thought the code would be cleaner if this is avoided. – Thangam May 26 '15 at 09:28
  • I think you can use `%%` ? – Moritz May 26 '15 at 18:41
1

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

Community
  • 1
  • 1
Thangam
  • 169
  • 3
  • 11