所以我正在绘制 pandas 数据框的错误栏。现在错误栏顶部有一个奇怪的箭头,但我想要的是一条水平线。例如,像这样的图: 但现在我的错误栏以箭头而不是水平线结束。
这是我用来生成它的代码:
plot = meansum.plot(
kind="bar",
yerr=stdsum,
colormap="OrRd_r",
edgecolor="black",
grid=False,
figsize=(8, 2),
ax=ax,
position=0.45,
error_kw=dict(ecolor="black", elinewidth=0.5, lolims=True, marker="o"),
width=0.8,
)
那么我应该改变什么才能使错误成为我想要的错误。谢谢。
最佳答案
使用 matplotlib 中的 plt.errorbar
会更容易,因为它会返回多个对象,包括 caplines
,其中包含您要更改的标记(当lolims
设置为 True
,请参阅 docs)。
使用pandas
,你只需要在plot
的 child 中挖掘正确的线并改变它的标记:
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
df = pd.DataFrame({"val":[1,2,3,4],"error":[.4,.3,.6,.9]})
meansum = df["val"]
stdsum = df["error"]
plot = meansum.plot(kind='bar',yerr=stdsum,colormap='OrRd_r',edgecolor='black',grid=False,figsize=8,2),ax=ax,position=0.45,error_kw=dict(ecolor='black',elinewidth=0.5, lolims=True),width=0.8)
for ch in plot.get_children():
if str(ch).startswith('Line2D'): # this is silly, but it appears that the first Line in the children are the caplines...
ch.set_marker('_')
ch.set_markersize(10) # to change its size
break
plt.show()
关于python - 更改 Pandas 条形图中的错误条标记(Caplines),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45204202/