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

Slowat_Kela:

我正在尝试复制示例,除了作为水平条形图。

我写了这段代码:

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')

当我不添加任何width参数时,脚本将运行,但是在输出中,条形图并不相邻:

因为我希望条形图彼此相邻(即Fam项将两个条形配对在一起-AllTested和BestSubsetTested-,每个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)

谁能帮我?

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

  • the bars are in pairs (i.e. item 1 has two bars, item2 has two bars)

  • the name for each pair of bars should be in the middle of it

  • if you could possibly show me how to tilt the name of each bar 45/make the font bigger so it's more readable that would be great because I shortened the names for this example.

tmdavison :

For barh, the width of the bars is controlled by the height argument, not width. Here I set height to 0.4 for both barh calls, and offset them by +/- 0.25. You don't need to offset the yticks if you offset the bars up and down as I do here.

To rotate the tick labels, you can add rotation=45 to the yticks function.

And to make the tick labels bigger, I just reduced the figure size from (60, 40) to (12, 8). An alternative would be to change the fontsize for all the tick labels and axes labels.

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

更新Matplotlib条形图

Matplotlib,水平条形图(barh)上下颠倒

Matplotlib条形图条形图重叠

条形图-两个条形图彼此相邻并且在单独的窗口中绘制条形图

Matplotlib条形图不绘制边框/边缘

为什么Python Matplotlib条形图的X轴刻度显示奇怪和错误的负值?

matplotlib中从右到左的水平条形图

条形图-两个条形图彼此相邻并且在单独的窗口中绘制条形图

为什么Matplotlib开始绘制条形图而不是折线图

尝试创建不连续轴时,为什么我的条形图会变成线形图?

如何修改ggplot2代码,使条形图彼此相邻而不是堆叠?

无法在每个水平条形图的顶部添加值

Matplotlib:堆积条形图

条形图的水平轴

Matplotlib并排条形图

熊猫:带相邻条形图的堆积条形图

熊猫/ matplotlib中的堆叠式交错水平条形图

在R中绘制两个彼此相邻的条形图

为什么堆积的水平条形图不能以不同的颜色显示某些值?

matplotlib轴条形图由于从熊猫df索引x轴而无法将条形图彼此相邻绘制

为什么我执行“ log”操作时,我的matplotlib条形图会压缩x轴

Seaborn 水平条形图

使用多个数据框绘制彼此相邻的条形图

水平条形图:不匹配的轴单位

Matplotlib 条形图,条形在彼此之上,如何创建空间

为什么这些条形图彼此重叠?

堆叠水平条形图

Matplotlib:为什么我的列表理解没有堆积条形图?

自定义多色水平条形图 matplotlib