我是matplotlab的初学者。对我来说,预期结果如下
我尝试根据需求制作代码,尝试将twinx()和set_aspect设置为'equal',但似乎不可能。以下是我的测试代码。我不知道如何设置参数以获得以下结果:
我也尝试过twinx,但无法将“ x轴和y轴缩放比例设置为1:1 ****”]
import numpy as np
import matplotlib.pyplot as plt
x1 = np.arange(10)
x2 = np.arange(20)
y1 = x1*x1*10000
y2 = np.sin(x2)
#adjust = 'datalim'
adjust = 'box'
#asp = 'equal'
asp = 'auto'
fig = plt.figure()
ax1 = fig.add_subplot(111,aspect=asp)
ax2 = fig.add_subplot(111,aspect=asp,frameon=False,sharex=ax1)
ax1.set_adjustable(adjust)
ax1.set_xlabel("xxxxxxxx")
ax1.set_ylabel("y1")
ax1.plot(x1,y1,label='ax1')
ax2.set_adjustable(adjust)
ax2.get_yaxis().set_ticks_position('right')
ax2.set_ylabel("y2",color='tab:red')
ax2.plot(x2,y2,label='ax2',color='tab:red')
ax1.tick_params(axis='y', labelcolor='tab:blue')
ax1.tick_params(axis='y', labelcolor='tab:red')
ax1.legend(loc='upper left', bbox_to_anchor=(1, 0.5))
ax2.legend(loc='upper left', bbox_to_anchor=(1, 0.6))
plt.show()
如果需要方形图形,只需将图形大小设置为两个相等的数字,例如
fig = plt.figure(figsize=[8, 8])
不管您的代码是什么,当您“通过鼠标更改图形大小”时,将发生什么情况取决于您使用的程序。