![matplotlib中的figsize不会更改图形大小吗?[重复],第1张 matplotlib中的figsize不会更改图形大小吗?[重复],第1张](/aiimages/matplotlib%E4%B8%AD%E7%9A%84figsize%E4%B8%8D%E4%BC%9A%E6%9B%B4%E6%94%B9%E5%9B%BE%E5%BD%A2%E5%A4%A7%E5%B0%8F%E5%90%97%EF%BC%9F%5B%E9%87%8D%E5%A4%8D%5D.png)
(由@tda提到的),也可能是最好的/最标准的方式一种选择,就是把
plt.figure前
plt.bar:
import matplotlib.pyplot as pltplt.figure(figsize=(20,10)) plt.bar(x['user'], x['number'], color="blue")
如果要在创建图形后设置图形大小,另一种选择是使用
fig.set_size_inches(请注意,我
plt.gcf在这里用来获取当前图形):
import matplotlib.pyplot as pltplt.bar(x['user'], x['number'], color="blue")plt.gcf().set_size_inches(20, 10)
尽管这不是最干净的代码,但可以一行完成所有 *** 作。首先,您需要创建图形,然后获取当前轴(
fig.gca),并在其上绘制条形图:
import matplotlib.pyplot as pltplt.figure(figsize=(20, 10)).gca().bar(x['user'], x['number'], color="blue")
最后,我将指出,通常最好使用matplotlib面向对象的方法,在该方法中,您可以保存对当前图形和轴的引用,并直接在其上调用所有绘图函数。它可能会添加更多行代码,但通常是更清晰的代码(并且您可以避免使用诸如
gcf()和
gca())。例如:
import matplotlib.pyplot as pltfig = plt.figure(figsize=(20, 10))ax = fig.add_subplot(111)ax.bar(x['user'], x['number'], color="blue")
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)