小编rys*_*ink的帖子

将名称放入Python条形图中的条形内

我有两个列表,其中一个是名称,另一个是值。我希望 y 轴为值,x 轴为名称。但是名称太长,无法放在轴上,这就是为什么我想将它们放入条形中,如图所示,但条形应该是垂直的。在此输入图像描述

在图片上,我的名单代表城市的名称。

我的输入是这样的:

mylist=[289.657,461.509,456.257]
nameslist=['Bacillus subtilis','Caenorhabditis elegans','Arabidopsis thaliana']
Run Code Online (Sandbox Code Playgroud)

我的代码:

fig = plt.figure()
width = 0.35
ax = fig.add_axes([1,1,1,1])
ax.bar(nameslist,mylist,width)
ax.set_ylabel('Average protein length')
ax.set_xlabel('Names')
ax.set_title('Average protein length by bacteria')  
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!

python matplotlib

3
推荐指数
1
解决办法
1866
查看次数

在 python 中关闭图形

我正在制作很多图并将它们保存到文件中,一切正常,但在编译过程中我收到以下消息:

RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  fig = self.plt.figure(figsize=self.figsize)
Run Code Online (Sandbox Code Playgroud)

所以我认为我可以通过关闭数字来改进代码,我用谷歌搜索并发现我应该使用fig.close(). 但是我收到以下错误'Figure' object has no attribute 'close'。我应该如何让它发挥作用?

这是我创建绘图的循环:

for i in years:
    ax = newdf.plot.barh(y=str(i), rot=0) 
    fig = ax.get_figure()
    fig.savefig('C:\\Users\\rysza\\Desktop\\python data analysis\\zajecia3\\figure'+str(i)+'.jpeg',bbox_inches='tight')
    fig.close()
Run Code Online (Sandbox Code Playgroud)

python matplotlib pandas

0
推荐指数
1
解决办法
5635
查看次数

标签 统计

matplotlib ×2

python ×2

pandas ×1