python 条形图与线图的图例_Matplotlib用线和图形标注条形图的方法

您可以使用较小的条形图作为目标和基准指标。Pandas不能自动为条添加注释,但是您可以简单地循环这些值并使用matplotlib的pyplot.annotate。在import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

a = np.random.randint(5,15, size=5)

t = (a+np.random.normal(size=len(a))*2).round(2)

b = (a+np.random.normal(size=len(a))*2).round(2)

df = pd.DataFrame({"a":a, "t":t, "b":b})

fig, ax = plt.subplots()

df["a"].plot(kind='bar', ax=ax, legend=True)

df["b"].plot(kind='bar', position=0., width=0.1, color="lightblue",legend=True, ax=ax)

df["t"].plot(kind='bar', position=1., width=0.1, color="purple", legend=True, ax=ax)

for i, rows in df.iterrows():

plt.annotate(rows["a"], xy=(i, rows["a"]), rotation=0, color="C0")

plt.annotate(rows["b"], xy=(i+0.1, rows["b"]), color="lightblue", rotation=+20, ha="left")

plt.annotate(rows["t"], xy=(i-0.1, rows["t"]), color="purple", rotation=-20, ha="right")

ax.set_xlim(-1,len(df))

plt.show()

5b9a7635e442293b5c0c8882c627f7ee.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值