Matplotlib水平条形图(barh):为什么条形图位于顶部而不是彼此相邻?

2024-06-28 20:57:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图复制this示例,除了作为水平条形图

我写了这段代码:

import sys
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
import numpy as np

Fams = ['Item1','Item2','Item3','Item4','Item5','Item6','Item7','Item8','Item9','Item10','Item11','Item12','Item13','Item14','Item15','Item16','Item17','Item18','Item19','Item20','Item21']
AllTested = [3,3,3,3,4,4,4,4,4,4,4,5,5,6,6,7,9,9,10,10,27]
BestSubsetTested = [1,0,0,0,3,0,3,0,1,0,1,1,2,4,1,1,8,4,9,8,11]

plt.figure(figsize=[60,40])
X = np.arange(len(Fams))
plt.barh(X,AllTested,color='g')
plt.barh(X + 0.25,BestSubsetTested,color='b')
plt.yticks([i+0.25 for i in range(len(Fams))],Fams)

# Naming the x and y axis
plt.xlabel('Tests')
plt.ylabel('Fams')

plt.savefig('day2.png',format='png')

当我不添加任何宽度参数时,脚本将运行,但在输出中,这些条不会彼此相邻:

由于我希望两条线彼此相邻(即,Fam项目有两条线配对在一起-AllTested和BestSubseted-,每个Fam项目之间的间隙稍大,以明确这一点,类似于示例),我在示例中添加了“width”参数,但我得到了错误:

我得到一个错误,说:

Traceback (most recent call last):
  File "make_plot_species_multiple2.py", line 13, in <module>
    plt.barh(X,AllTested,color='g',width=0.25)

有人能帮我吗

最终输出应该与我所做的类似,除了

  • 钢筋成对(即项目1有两根钢筋,项目2有两根钢筋)

  • 每对条的名称应该在其

    的中间
  • 如果你能告诉我如何倾斜每个条的名称45/使字体更大,这样更可读,那将是非常好的,因为我缩短了这个例子的名称


Tags: 项目import名称示例lenmatplotlibasnp
2条回答
  • 我建议使用pandas来处理表格数据
  • ^{}使从多列数据创建条形图变得容易。
  • 一旦数据被保存,分析起来也就容易多了
import pandas as pd
import matplotlib.pyplot as plt

# using your data to create the dataframe
df = pd.DataFrame({'all_tested': AllTested, 'best_sub_tested': BestSubsetTested}, index=Fams)

# display(df.head())
       all_tested  best_sub_tested
Item1           3                1
Item2           3                0
Item3           3                0
Item4           3                0
Item5           4                3

# plot the dataframe
df.plot.barh(figsize=(16, 8))
plt.ylabel('Fams')
plt.xlabel('Tests')
plt.show()

enter image description here

对于barh,条的宽度由height参数控制,而不是由width参数控制。在这里,我将两个barh调用的height设置为0.4,并将它们偏移+/-0.25。如果你像我这里做的那样上下偏移这些条,你不需要偏移这些条

要旋转记号标签,可以将rotation=45添加到yticks函数中

为了使刻度标签更大,我只是将数字大小从(60,40)减小到(12,8)。另一种方法是更改所有记号标签和轴标签的字体大小

import sys
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
import numpy as np
plt.rcdefaults()

Fams = ['Item1','Item2','Item3','Item4','Item5','Item6','Item7','Item8','Item9','Item10','Item11','Item12','Item13','Item14','Item15','Item16','Item17','Item18','Item19','Item20','Item21']
AllTested = [3,3,3,3,4,4,4,4,4,4,4,5,5,6,6,7,9,9,10,10,27]
BestSubsetTested = [1,0,0,0,3,0,3,0,1,0,1,1,2,4,1,1,8,4,9,8,11]

plt.figure(figsize=[12, 8])
X = np.arange(len(Fams))
plt.barh(X-0.25, AllTested,color='g', height=0.4)
plt.barh(X+0.25, BestSubsetTested,color='b', height=0.4)
plt.yticks([i for i in range(len(Fams))],Fams, rotation=45)

# Naming the x and y axis
plt.xlabel('Tests')
plt.ylabel('Fams')

plt.savefig('day2.png',format='png')

enter image description here

相关问题 更多 >