尝试使用两种方式模仿作图。
方法一:
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
%config InlineBackend.figure_format = 'svg'
list1=[['Jim',13.0,1],['Slim',8.4,2],['Harry',9.0,3],['Dick',11.0,4],['Tom',10.0,5]]
df=pd.DataFrame(list1,columns=['name','num','index'])
fig, ax = plt.subplots(figsize=(8,5))
err=[0.72,0.70,0.16,0.25,0.71]
err_attr={"elinewidth":2,"ecolor":"black","capsize":3}
plt.barh(df['index'], df['num'],xerr=err,error_kw=err_attr,tick_label=list(df['name']))
plt.xlim(0,16)
plt.suptitle("How fast do you want to go today?",y=0.92)
plt.xlabel('Performance')
ax.text(11, 5, '±0.71',c='blue',size=12)
ax.text(11.6, 4, '±0.25',c='blue',size=12)
ax.text(9.5, 3, '±0.16',c='blue',size=12)
ax.text(9.5, 2, '±0.70',c='blue',size=12)
ax.text(14.2, 1, '±0.72',c='blue',size=12)
plt.show()
运行结果:
方法二:
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
%config InlineBackend.figure_format = 'svg'
list1=[['Jim',13.0,1],['Slim',8.4,2],['Harry',9.0,3],['Dick',11.0,4],['Tom',10.0,5]]
df=pd.DataFrame(list1,columns=['name','num','index'])
fig, ax = plt.subplots(figsize=(8,5))
err=[0.72,0.70,0.16,0.25,0.71]
err_attr={"elinewidth":2,"ecolor":"black","capsize":3}
hbars=plt.barh(df['index'], df['num'],xerr=err,error_kw=err_attr,tick_label=list(df['name']))
plt.xlim(0,16)
plt.suptitle("How fast do you want to go today?",y=0.92)
plt.xlabel('Performance')
# Label with given captions, custom padding and annotate options
ax.bar_label(hbars, labels=['±%.2f' % e for e in err],
padding=8, color='b', fontsize=14)
plt.show()
运行结果:
参考资料:
1.第四回:文字图例尽眉目
2.Lines, bars and markers