带有错误栏和标记的熊猫图折线图

时间:2018-08-27 08:15:33

标签: python pandas matplotlib

我正在尝试使用pandas.plot来绘制折线图,​​该折线图应同时包含标记和误差线。但是由于某些原因,如果我指定yerr值,则不会显示标记。

这些是数据帧:

df = pd.DataFrame({
'Time': [0, 5, 10, 15, 20, 25],
'Capomulin': [45.0, 44.26608641544399, 43.08429058188399, 42.06431734681251, 40.71632532212173, 39.93952782686818],
'Infubinol': [45.0, 47.062001033088, 49.40390857087143, 51.29639655633334, 53.19769093422999, 55.71525236228889],
'Ketapril': [45.0, 47.38917452114348, 49.582268974622714, 52.39997374321578, 54.92093473734737, 57.678981717731574],
'Placebo': [45.0, 47.125589188437495, 49.42332947868749, 51.35974169802999, 54.36441702681052, 57.48257374394706]})
df.set_index('Time', inplace=True)

errors = pd.DataFrame({
'Time': [0, 5, 10, 15, 20, 25],
'Capomulin': [0.0, 0.44859285020103756, 0.7026843745238932, 0.8386172472985688, 0.9097306924832056, 0.8816421535181787],
'Infubinol': [0.0, 0.23510230430767506, 0.2823459146215716, 0.35770500497539054, 0.4762095134790833, 0.5503145721542003],
'Ketapril': [0.0, 0.26481852016728674, 0.35742125637213723, 0.5802679659678779, 0.7264838239834724, 0.7554127528910378],
'Placebo': [0.0, 0.21809078325219497, 0.40206380730509245, 0.6144614435805993, 0.8396091719248746, 1.0348719877946384]})
errors.set_index('Time', inplace=True)

当我进行无误打印时,会发生以下情况:

df.plot(figsize=(12,8), 
        style=['^-', 'o--', 'x-.', 'D-'], 
        markersize=14)

enter image description here

这是带有错误栏:

df.plot(figsize=(12, 8), 
        style=['^-', 'o--', 'x-.', 'D-'], 
        yerr=errors,
        markersize=14)

enter image description here

那我该如何绘制它们呢?

UPD :将示例数据作为数据框

环境:操作系统-Win10 x64,pandas 0.23,matplotlib 2.2.2

1 个答案:

答案 0 :(得分:2)

好吧,我可以确认在具有TkAgg后端的Ubuntu 18.04,pandas 0.23.4,matplotlib 2.2.3中也会发生这种情况。我不确定这是错误还是功能,但是您可以模拟预期的行为:

from matplotlib import pyplot as plt
import pandas as pd

#create your sample data
df = pd.DataFrame({
'Time': [0, 5, 10, 15, 20, 25],
'Capomulin': [45.0, 44.26608641544399, 43.08429058188399, 42.06431734681251, 40.71632532212173, 39.93952782686818],
'Infubinol': [45.0, 47.062001033088, 49.40390857087143, 51.29639655633334, 53.19769093422999, 55.71525236228889],
'Ketapril': [45.0, 47.38917452114348, 49.582268974622714, 52.39997374321578, 54.92093473734737, 57.678981717731574],
'Placebo': [45.0, 47.125589188437495, 49.42332947868749, 51.35974169802999, 54.36441702681052, 57.48257374394706]})
df.set_index('Time', inplace=True)

errors = pd.DataFrame({
'Time': [0, 5, 10, 15, 20, 25],
'Capomulin': [0.0, 0.44859285020103756, 0.7026843745238932, 0.8386172472985688, 0.9097306924832056, 0.8816421535181787],
'Infubinol': [0.0, 0.23510230430767506, 0.2823459146215716, 0.35770500497539054, 0.4762095134790833, 0.5503145721542003],
'Ketapril': [0.0, 0.26481852016728674, 0.35742125637213723, 0.5802679659678779, 0.7264838239834724, 0.7554127528910378],
'Placebo': [0.0, 0.21809078325219497, 0.40206380730509245, 0.6144614435805993, 0.8396091719248746, 1.0348719877946384]})
errors.set_index('Time', inplace=True)

#plot error bars
ax = df.plot(figsize=(12,8), yerr = errors, legend = False)
#reset color cycle so that the marker colors match
ax.set_prop_cycle(None)
#plot the markers
df.plot(figsize=(12,8), style=['^-', 'o--', 'x-.', 'D-'], markersize=14, ax = ax)

plt.show()

样本输出: enter image description here