gpt4 book ai didi

python - 创建一个条形图,其条形是相对于最大值或最小值的填充进度百分比

转载 作者:行者123 更新时间:2023-11-28 17:59:30 30 4
gpt4 key购买 nike

我有经过热处理循环的 Material 的机械性能数据。条形图按每个循环分组,具有三个属性:屈服、拉伸(stretch)和伸长率。屈服强度和抗拉强度共享同一 y 轴,而伸长率位于第二个 y 轴上。屈服强度和抗拉强度具有最大值,而伸长率具有最小值。我不希望将引用线用于最小值和最大值,而是希望将条形图填充为最大最小值的百分比。

我正在使用 pandas 创建带有 .plot 的数据框,同时使用 color=None 和 edgecolor 制作“空”条。但是,edgecolor 设置每个组的颜色。

我尝试过孵化和填充,但一直没能弄清楚如何只填充其中的一部分。

我也不希望 df["y_norm"] 出现在每个组中。此列仅包含栏应填充颜色的比例。

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from io import StringIO

s=StringIO(""" Yield UTS Elongation
T1 10.5 25.3 30.2
T2 10.8 26.3 30.3
T3 11.0 26.5 30.2
T4 11.5 27.2 30.4
T5 20.1 30.2 22.3
T6 24.7 31.2 19.0
T7 19.0 27.1 19.6
T8 12.2 21.7 23.4
T9 8.00 18.3 31.4""")

Ymax=float(16.0) #Yield strength maximum limit
UTSmax=float(22.0) #Ultimate Tensile Strengh maximum limit
Elmin=float(22.0) #Percent Elongation minimum limit

df = pd.read_csv(s, index_col=0, delimiter=' ',skipinitialspace=True)

df_scale=pd.DataFrame()
df_scale["y_norm"]=round(Ymax/df["Yield"],2)
df_scale["y_norm"]=df_scale["y_norm"].where(df_scale["y_norm"] < 1, 1)

#--progres bar for Ultimate Tensile Strength Maximum Limit-----
df_scale["T_norm"]=round(UTSmax/df["UTS"],2)
df_scale["T_norm"]=df_scale["T_norm"].where(df_scale["T_norm"]<1,1)

#--progres bar for Elongation Minimum Limit-----
df_scale["El_norm"]=round(Elmin/df["Elongation"],2)
df_scale["El_norm"]=df_scale["El_norm"].where(df_scale["El_norm"]>1,1)
df_scale["El_norm"]=df_scale["El_norm"].where(df_scale["El_norm"]
<1,abs(df_scale["El_norm"]-2))

M_props=df.plot(kind="bar",secondary_y="Elongation",rot=0,
edgecolor="rgb",color="None")
M_props.set_ylabel('KSI',rotation=0)
M_props.right_ax.set_ylabel('% El')
M_props.right_ax.set_ylim([10,40])

plt.show()

结果应该只显示最后两组,T8 和 T9,作为完全填充的条。

最佳答案

IIUC,你需要再次乘以y_norm并重新绘制:

# columns to draw:
col2draw = df.columns[:-1]

fig, M_props = plt.subplots(figsize=(10,6))

# draw the full-length empty bars
df[col2draw].plot(kind="bar",secondary_y="Elongation",
rot=0, edgecolor="k", ax=M_props,
color="None",
legend=False)

# fill the bars based on y_norm
(df[col2draw].apply(lambda x: x*df['y_norm'])
.plot(kind="bar",
secondary_y="Elongation",
rot=0,
ax=M_props)
)

M_props.set_ylabel('KSI',rotation=0)
M_props.right_ax.set_ylabel('% El')
M_props.right_ax.set_ylim([10,40])
plt.show()

输出:

enter image description here


更新:如果列的比例不同:

# create a scale dataframe, note the columns and index
df_scale = pd.DataFrame(columns=df.columns,
index=df.index)

# compute the scales, here we assign it randomly with seed
np.random.seed(1)
df_scale = np.random.uniform(0.5,1,(len(df),3))

# plot as previously
fig, M_props = plt.subplots(figsize=(10,6))

# draw the full-length empty bars
df.plot(kind="bar",secondary_y="Elongation",
rot=0, edgecolor="k", ax=M_props,
color="None",
legend=False)

# fill the bars based on y_norm
((df*df_scale).plot(kind="bar",
secondary_y="Elongation",
ax=M_props)
)

M_props.set_ylabel('KSI',rotation=0)
M_props.right_ax.set_ylabel('% El')
M_props.right_ax.set_ylim([10,40])
plt.show()

输出: enter image description here

关于python - 创建一个条形图,其条形是相对于最大值或最小值的填充进度百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56436360/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com