您似乎想使用具有指定比例的寄生虫轴。matlpotlib 站点上有一个这样的例子,稍作修改的版本如下。
import matplotlib.transforms as mtransforms
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.parasite_axes import SubplotHost
import numpy as np
# Set seed for random numbers generator to make data recreateable
np.random.seed(1235)
# Define data to be plotted
x1 = np.arange(0,10000,1000)
x2 = x1/1000.
y1 = np.random.randint(0,10,10)
y2 = y1/5.
# Create figure instance
fig = plt.figure()
# Make AxesHostAxesSubplot instance
ax = SubplotHost(fig, 1, 1, 1)
# Scale for top (parasite) x-axis: makes top x-axis 1/1000 of bottom x-axis
x_scale = 1000.
y_scale = 1.
# Set scales of parasite axes to x_scale and y_scale (relative to ax)
aux_trans = mtransforms.Affine2D().scale(x_scale, y_scale)
# Create parasite axes instance
ax_parasite = ax.twin(aux_trans)
ax_parasite.set_viewlim_mode('transform')
fig.add_subplot(ax)
# Plot the data
ax.plot(x1, y1)
ax_parasite.plot(x2, y2)
# Configure axis labels and ticklabels
ax.set_xlabel('Original x-axis')
ax_parasite.set_xlabel('Parasite x-axis (scaled)')
ax.set_ylabel('y-axis')
ax_parasite.axis['right'].major_ticklabels.set_visible(False)
plt.show()
这给出了下面的输出
如果您更改ax
实例的限制,实例的限制ax_parasite
会自动更新:
# Set limits of original axis (parasite axis are scaled automatically)
ax.set_ylim(0,12)
ax.set_xlim(500,4000)