【问题标题】:using a pdf image inside a subplot in matplotlib在 matplotlib 的子图中使用 pdf 图像
【发布时间】:2015-08-06 21:04:11
【问题描述】:

我希望在 matplotlib 的子图中有一个 pdf(最好是)图像,以显示周期不同阶段的图。我尝试使用imshow,但无法将其放入子图中。有没有办法做到这一点?此刻,我不得不将子图的 pdf 导入到 inkscape 并编辑矢量图形以添加这些曲线位置!并且很难创建正确的对齐方式!希望有任何建议。

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)

【问题讨论】:

  • 我并没有真正得到你想要的东西。哪个周期?你的数据是什么?你目前是怎么做的?
  • 抱歉,如果我的问题不够清楚。通过周期,我的意思是每个子图顶部的小 pdf 表示百分比。我可以绘制子图(我的数据),但顶部的这些循环指示器是绘制的 pdf,并使用 inkscape 放置在顶部!我想知道我是否可以将这个循环指示 pdf 与 matplotlib 放在顶部的子图中。
  • 请发布您的代码
  • 发布了我试图将图像添加到子图中的代码部分。

标签: python pdf image-processing matplotlib


【解决方案1】:

这就是我调整Moritz的代码的方式:

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------------------

【讨论】:

    【解决方案2】:

    也许作为一个开始的想法:

    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()
    

    【讨论】:

    • 我几乎完成了它,它可以进行一些调整。另外,如何在代码的最后一行中使用符号 % 而不是单词“Percent”?我尝试转换为原始字符串,但在注释中转义这个符号太棘手了!有什么想法吗?如果那也解决了,我可以发布代码!否则我每次都必须将它更改为原始字符串以获得我想要的 % 。我认为如果避免这种情况,代码会更干净。
    • 我觉得你可以用%% ?
    猜你喜欢
    • 2015-10-20
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 2013-12-04
    • 2020-12-19
    • 1970-01-01
    相关资源
    最近更新 更多