pandas 如何使用条形图绘制最小/最大条形

llycmphe  于 2023-03-06  发布在  其他
关注(0)|答案(3)|浏览(204)

我想修改我的绘图代码,以便显示下图所示的最小/最大条:
我的代码是:

from datetime import datetime, timedelta
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("white")
sns.set_style('darkgrid',{"axes.facecolor": ".92"}) # (1)
sns.set_context('notebook')

Delay = ['S1', 'S2', 'S3', 'S4']

Time = [87, 66, 90, 55]

df = pd.DataFrame({'Delay':Delay,'Time':Time})
print("Accuracy")

display(df) # in jupyter

fig, ax = plt.subplots(figsize = (8,6))

x = Delay
y = Time

plt.xlabel("Delay", size=14)
plt.ylim(-0.3, 100)
width = 0.1

for i, j in zip(x,y): 
    ax.bar(i,j, edgecolor = "black",
        error_kw=dict(lw=1, capsize=1, capthick=1))  
    ax.set(ylabel = 'Accuracy')

from matplotlib import ticker
ax.yaxis.set_major_locator(ticker.MultipleLocator(10)) 
plt.savefig("Try.png", dpi=300, bbox_inches='tight')

代码生成此图:

我要添加的最小值/最大值适用于:

87 (60-90)
66 (40-70)
90 (80-93)
55 (23-60)

先谢谢你的帮助。

ffdz8vbo

ffdz8vbo1#

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# set edgecolor param (this is a global setting, so only set it once)
plt.rcParams["patch.force_edgecolor"] = True

# setup the dataframe
Delay = ['S1', 'S2', 'S3', 'S4']

Time = [87, 66, 90, 55]

df = pd.DataFrame({'Delay':Delay,'Time':Time})

# create a dict for the errors
error = {87: {'max': 90,'min': 60}, 66: {'max': 70,'min': 40}, 90: {'max': 93,'min': 80}, 55: {'max': 60,'min': 23}}

∮ ∮ ∮ ∮ ∮

  • seaborn.barplot将自动添加误差线,如链接中的示例所示。但是,这是特定于使用许多数据点的。在这种情况下,将一个值指定为误差,而不是根据数据确定误差。
  • 以这种方式添加误差线时,可以指定capsize参数,以便在误差线的顶部和底部添加水平线。
# plot the figure
fig, ax = plt.subplots(figsize=(8, 6))
sns.barplot(x='Delay', y='Time', data=df, ax=ax)

# add the lines for the errors 
for p in ax.patches:
    x = p.get_x()  # get the bottom left x corner of the bar
    w = p.get_width()  # get width of bar
    h = p.get_height()  # get height of bar
    min_y = error[h]['min']  # use h to get min from dict z
    max_y = error[h]['max']  # use h to get max from dict z
    plt.vlines(x+w/2, min_y, max_y, color='k')  # draw a vertical line

  • gepcel中的answer所示,yerr参数可用于显式地向API提供错误。
  • 但是,错误的格式对于参数不正确。yerr要求值与条形的顶部相关
  • S1为87,min为60,max为90。因此,ymin为27(87 - 60),ymax为3(90 - 87)。
  • seaborn.barplotcapsize参数似乎不适用于yerr,因此必须设置matplotlib'errorbar.capsize'rcParmas
# set capsize param (this is a global setting, so only set it once)
plt.rcParams['errorbar.capsize'] = 10

# create dataframe as shown by gepcel
Delay = ['S1', 'S2', 'S3', 'S4']

Time = [87, 66, 90, 55]
_min = [60, 40, 80, 23]
_max = [90, 70, 93, 60]
df = pd.DataFrame({'Delay':Delay,'Time':Time, 'Min': _min, 'Max': _max})

# create ymin and ymax
df['ymin'] = df.Time - df.Min
df['ymax'] = df.Max - df.Time

# extract ymin and ymax into a (2, N) array as required by the yerr parameter
yerr = df[['ymin', 'ymax']].T.to_numpy()

# plot with error bars
fig, ax = plt.subplots(figsize=(8, 6))
sns.barplot(x='Delay', y='Time', data=df, yerr=yerr, ax=ax)

∮ ∮ ∮ ∮ ∮

fig, ax = plt.subplots(figsize=(8, 6))

df.plot.bar(x='Delay', y='Time', ax=ax)

for p in ax.patches:
    x = p.get_x()  # get the bottom left x corner of the bar
    w = p.get_width()  # get width of bar
    h = p.get_height()  # get height of bar
    min_y = error[h]['min']  # use h to get min from dict z
    max_y = error[h]['max']  # use h to get max from dict z
    plt.vlines(x+w/2, min_y, max_y, color='k')  # draw a vertical line

一米二十四分一秒

fig, ax = plt.subplots(figsize=(8, 6))

ax.bar(x='Delay', height='Time', data=df)

for p in ax.patches:
    x = p.get_x()  # get the bottom left x corner of the bar
    w = p.get_width()  # get width of bar
    h = p.get_height()  # get height of bar
    min_y = error[h]['min']  # use h to get min from dict z
    max_y = error[h]['max']  # use h to get max from dict z
    plt.vlines(x+w/2, min_y, max_y, color='k')  # draw a vertical line

tzdcorbm

tzdcorbm2#

你可以直接使用plt.baryerr参数,以@特伦顿McKinney的代码为例:

import pandas as pd
import matplotlib.pyplot as plt

# setup the dataframe
Delay = ['S1', 'S2', 'S3', 'S4']

Time = [87, 66, 90, 55]
_min = [60, 40, 80, 23]
_max = [90, 70, 93, 60]
df = pd.DataFrame({'Delay':Delay,'Time':Time, 'Min': _min, 'Max': _max})
df = (df.assign(yerr_min = df.Time-df.Min)
        .assign(yerr_max=df.Max-df.Time))

plt.figure(figsize=(8, 6))
plt.bar(x='Delay', height='Time', yerr=df[['yerr_min', 'yerr_max']].T.values, capsize=10, data=df)

plt.show()

vybvopom

vybvopom3#

下面是一个使用yerrnumpy的解决方案,它比@gepcel的样板代码少。

import matplotlib.pyplot as plt
import numpy as np

Delay = ['S1', 'S2', 'S3', 'S4']   # Categories

Time = [87, 66, 90, 55]
_min = [60, 40, 80, 23]
_max = [90, 70, 93, 60]

plt.figure(figsize=(8, 6))

yerr = [np.subtract(Time, _min), np.subtract(_max, Time)]
plt.bar(Delay, Time, yerr=yerr, capsize=10)

plt.show()

相关问题