问题描述
我目前附有图表.我希望在该图下方显示一个单独的图,以显示两行之间的差异,以直观地显示错误分析的功能方法.
I currently have the graph attached. I want a separate graph underneath this one showing the difference between the two lines to visually show the functional approach to error analysis.
如何在不影响当前图形的大小和形状的情况下在此图形下方添加带有空格和新轴的子图.
How do I add a subplot below this graph with a space and new axes without effecting the size and shape of my current graph.
我当前的代码是:
fig, ax = plt.subplots()
plt.plot(x, y1, label = 'label1')
plt.plot(x, y2, label = 'label2')
plt.plot(x, y3, label = 'label2')
plt.xlabel(u'Redshift (z)', fontname = 'Times New Roman', size = 15)
plt.ylabel(u'Look-back Time (yrs)', fontname = 'Times New Roman', size = 15)
legend = plt.legend(loc='lower right', shadow=True, fontsize='large')
axins = zoomed_inset_axes(ax, 4.5, loc=10)
axins.plot(x, y1, label = 'label1')
axins.plot(x, y2, label = 'label2')
axins.plot(x, y3, label = 'label2')
x1, x2, y1, y2 = 2, 2.5, 1.25*10**10, 1.3*10**10
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
mark_inset(ax, axins, loc1=1, loc2=3, fc="none", ec="0.5")
plt.show()
当我尝试在下面添加子图时,我使用了以下代码:
When I try to add a subplot below I used this code:
plt.subplot(212)
plt.plot(yaxis, difference(y2, y3))
plt.subplots_adjust(bottom = 0.1, hspace=5)
它只是在新图形中创建了一个较小的图.我希望一个在另一个下面.
It just creates a smaller plot in a new figure. Where as I want one to be underneath the other.
推荐答案
您的想法是正确的,但是您需要在调用plt.show()之前添加一个新的子图.这是因为在那个调用之后,图形的实例在你关闭它之后被杀死了.
Your idea is correct, but you need to add a new subplot before calling for plt.show(). This is because after that call, the instance of the figure is killed after you close it.
通常,您可能希望直接调用图形实例的 add_subplot() 方法(在您的情况下为 fig.add_subplot()),因为这样您就可以确保将其添加到所需的对象中.
In general, you may want to call directly the method add_subplot() of your figure instance (in your case fig.add_subplot()) because then you make sure it's added to the desired object.
这篇关于在带有Python中插入图的图下创建子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!