我使用绘制了一系列相关函数matplotlib.pyplot.subplots
,并且需要在每个子图中包括相应函数的放大部分。
我开始按照此处的说明进行操作,当只有一个图形但没有子图时,它可以完美地工作。
如果我用子图来做的话,我只会得到一个图,里面有所有函数。这是到目前为止我得到的一个例子:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10, 10, 0.01)
sinx = np.sin(x)
tanx = np.tan(x)
fig, ax = plt.subplots( 1, 2, sharey='row', figsize=(9, 3) )
for i, f in enumerate([sinx, cosx]):
ax[i].plot( x, f, color='red' )
ax[i].set_ylim([-2, 2])
axx = plt.axes([.2, .6, .2, .2],)
axx.plot( x, f, color='green' )
axx.set_xlim([0, 5])
axx.set_ylim([0.75, 1.25])
plt.show(fig)
这段代码给出了下图:
如何创建新轴并在每个子图中绘图?
如果我了解得很好,您可以使用 inset_axes
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.inset_locator import inset_axes
x = np.arange(-10, 10, 0.01)
sinx = np.sin(x)
tanx = np.tan(x)
fig, ax = plt.subplots( 1, 2, sharey='row', figsize=(9, 3) )
for i, f in enumerate([sinx, tanx]):
ax[i].plot( x, f, color='red' )
ax[i].set_ylim([-2, 2])
# create an inset axe in the current axe:
inset_ax = inset_axes(ax[i],
height="30%", # set height
width="30%", # and width
loc=10) # center, you can check the different codes in plt.legend?
inset_ax.plot(x, f, color='green')
inset_ax.set_xlim([0, 5])
inset_ax.set_ylim([0.75, 1.25])
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句