![](/img/trans.png)
[英]using matplotlib giving me the following warning: "UserWarning: tight_layout:
[英]ValueError when using matplotlib tight_layout()
好的,这是我第一次在这里问一个问题,所以请耐心等待我;-)
我正在尝试使用matplotlib在图中创建一系列子图(每个图中有两个y轴),然后保存该图。 我正在使用GridSpec为子图创建一个网格,并意识到它们重叠了一点,这是我不想要的。 所以我正在尝试使用tight_layout()对其进行排序,根据matplotlib文档应该可以正常工作。 稍微简化一下,我的代码看起来像这样:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(num=None, facecolor='w', edgecolor='k')
grid = gridspec.GridSpec(2, numRows)
# numRows comes from the number of subplots required
# then I loop over all the data files I'm importing and create a subplot with two y-axes each time
ax1 = fig.add_subplot(grid[column, row])
# now I do all sorts of stuff with ax1...
ax2 = ax1.twinx()
# again doing some stuff here
在完成数据处理循环并创建了所有子图之后,我最终结束了
fig.tight_layout()
fig.savefig(str(location))
至于我可以解决,这应该工作,但是当调用tight_layout()时,我从函数self.subplotpars得到一个ValueError:left不能> = right。 我的问题是:我如何找出导致此错误的原因以及如何解决?
我以前遇到过这个错误,我有一个适合我的解决方案。 我不确定它是否适合你。 在matplotlib中,命令
plt.fig.subplots_adjust()
可以用来拉伸情节。 左侧和底部伸展越多,数字越小,而顶部和右侧伸展越多,数字越大。 因此,如果left大于或等于右边,或者bottom大于或等于顶部,则图形会翻转。 我调整了我的命令看起来像这样:
fig = plt.figure()
fig.subplots_adjust(bottom = 0)
fig.subplots_adjust(top = 1)
fig.subplots_adjust(right = 1)
fig.subplots_adjust(left = 0)
然后你可以填写你自己的数字来调整它,只要你保持左边和底部较小。 我希望这可以解决你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.