Pandas用误差线和标记绘制线图

2024-06-28 10:55:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用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)

当我没有错误的时候会发生什么:

^{pr2}$

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


Tags: 数据标记dataframepandasdftimeplot错误
1条回答
网友
1楼 · 发布于 2024-06-28 10:55:39

好吧,我可以确认在Ubuntu18.04、pandas 0.23.4、matplotlib 2.2.3和TkAgg后端中也会发生这种情况。我不确定这是一个bug还是一个特性,但是您可以模拟预期的行为:

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

相关问题 更多 >