我对新的 Matplotlib 2.0.0 有疑问。
默认情况下,新的 autolimit_mode 值会向框架添加填充。
我想避免它变成一个数字。
如果我更改 rcParams,则更改会影响任何生成的图形。
我可以更改单个图形的此参数而不影响其余图形的行为吗?
Update
我用一些代码更新了问题并获得了不同的结果。
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
def remove_frame_border(ax):
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
def draw1(xserie, yserie):
fig = plt.figure(figsize=(8,3))
ax = fig.add_subplot(111)
ax.plot(xserie, yserie)
ax.grid(True)
remove_frame_border(ax)
fig.savefig('out1.png', format='png', bbox_inches='tight', pad_inches=0)
def draw2(xserie, yserie):
fig = plt.figure(figsize=(8,3))
ax = fig.add_subplot(111)
# Fixed autolimit padding in matplotlib 2.0.0
ax.set_xmargin(0)
ax.set_ymargin(0)
ax.autoscale_view()
ax.plot(xserie, yserie)
ax.grid(True)
remove_frame_border(ax)
# Restore default matplotlib params
matplotlib.rcParams.update(matplotlib.rcParamsDefault)
fig.savefig('out2.png', format='png', bbox_inches='tight', pad_inches=0)
def draw3(xserie, yserie):
# Fixed autolimit padding in matplotlib 2.0.0
matplotlib.rcParams['axes.autolimit_mode'] = 'round_numbers'
matplotlib.rcParams['axes.xmargin'] = 0
matplotlib.rcParams['axes.ymargin'] = 0
fig = plt.figure(figsize=(8,3))
ax = fig.add_subplot(111)
ax.plot(xserie, yserie)
ax.grid(True)
remove_frame_border(ax)
# Restore default matplotlib params
matplotlib.rcParams.update(matplotlib.rcParamsDefault)
fig.savefig('out3.png', format='png', bbox_inches='tight', pad_inches=0)
xserie = np.array([1,2,3,4,5,6,7,8,9,10])
yserie = np.array([0, 22.2, 33.4, 55.6, 66.6, 77.7, 100.3, 123.3, 90.4, 93.3])
draw1(xserie, yserie)
draw2(xserie, yserie)
draw3(xserie, yserie)
- draw1 是默认行为
- draw2 调用 ax.set_xmargin 和 ax.set_ymargin
- draw3更改rcParams并在生成图表后恢复它
out1.png
out2.png
out3.png