如何更改具有相同宽高比的地块大小?

时间:2015-11-06 08:51:09

标签: python matplotlib

似乎我不能同时设置等轴刻度和设置绘图的大小。我正在做的是:

fig = pl.figure(figsize=(20,20))
ax = fig.add_subplot(111)
ax.set_aspect('equal')

如果我移除figsize情节似乎具有相同的比例,但是figsize我的情节更大但是尺度不再相等。

编辑:图表不一定是正方形,只是更大..请假设我现在没有轴的确切比例

任何解决方案?

由于

1 个答案:

答案 0 :(得分:0)

如果您想更改数据限制以使轴成方形,请在datalim的通话中添加set_aspect

ax.set_aspect('equal', 'datalim')

如果您希望缩放更改以使限制不同但轴看起来是方形,则可以计算轴尺寸比并明确设置:

ax.set_aspect(1./ax.get_data_ratio())

e.g。

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111)

x = np.linspace(0,np.pi,1000)
y = np.sin(3*x)**2
ax.plot(x,y)
ax.set_aspect('equal', 'datalim')
plt.show()

enter image description here

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111)

x = np.linspace(0,np.pi,1000)
y = np.sin(3*x)**2
ax.plot(x,y)
ax.set_aspect(1./ax.get_data_ratio())
plt.show()

enter image description here

相关问题