如何限制matplotlib图上的边框大小?
-
05-07-2019 - |
题
我正在制作一些非常大的图形,并且边框中的空白占据了大量像素,这些像素将被数据更好地使用。随着图表的增长,边界似乎也在增长。
以下是我的图表代码的内容:
import matplotlib
from pylab import figure
fig = figure()
ax = fig.add_subplot(111)
ax.plot_date((dates, dates), (highs, lows), '-', color='black')
ax.plot_date(dates, closes, '-', marker='_', color='black')
ax.set_title('Title')
ax.grid(True)
fig.set_figheight(96)
fig.set_figwidth(24)
有没有办法减少边框的大小?也许某个地方的设置可以让我保持边界恒定2英寸左右?
解决方案
由于看起来您只是使用单个子图,您可能想跳过 add_subplot
并直接转到 add_axes
。这将允许您给出轴的大小(在图形相对坐标中),因此您可以在图中使其大小。在您的情况下,这意味着您的代码看起来像
import matplotlib.pyplot as plt
fig = plt.figure()
# add_axes takes [left, bottom, width, height]
border_width = 0.05
ax_size = [0+border_width, 0+border_width,
1-2*border_width, 1-2*border-width]
ax = fig.add_axes(ax_size)
ax.plot_date((dates, dates), (highs, lows), '-', color='black')
ax.plot_date(dates, closes, '-', marker='_', color='black')
ax.set_title('Title')
ax.grid(True)
fig.set_figheight(96)
fig.set_figwidth(24)
如果需要,您甚至可以直接在 figure()
调用中将参数放到 set_figheight
/ set_figwidth
。
其他提示
尝试 subplots_adjust
API:
subplots_adjust(* args,** kwargs)
fig.subplots_adjust(left = None,bottom = None,right = None,wspace = None,hspace = None)
使用kwargs更新SubplotParams(默认为rc,其中为None)并更新子图位置
不隶属于 StackOverflow