我正在尝试复制此示例,除了作为水平条形图。
我写了这段代码:
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.
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')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句