Matplotlib学习笔记

尝试使用两种方式模仿作图。

方法一:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值