问题
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.
My current code is:
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.
回答1:
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.
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.
来源:https://stackoverflow.com/questions/35710402/creating-a-subplot-below-a-plot-with-an-inset-graph-in-python