我正在嘗試使用堆疊條形圖和分類條形圖的組合創建混合圖表。
這是示例資料。
df = pd.DataFrame({
'Categories': ["Two Instances", "Two Instances", "Two Instances"],
'X': ["FS", "HRRS", "Sequential"],
'Y': [146, 82, 80],
'Y1': [157, 160, 162]
})

這是當前的條形圖代碼,分別是要繪制的
ax1 = plt.subplots()
ax1 = sns.barplot(x=df["X"], y=df["Y1"], color='#590D22', label="T2")
ax1 = sns.barplot(x=df["X"], y=df["Y"], color='#800F2F', label="T1")
plt.legend(bbox_to_anchor=(1, 1.02), loc='lower right', borderaxespad=0)
plt.show()

我正在嘗試將前 2 個條形圖并排而不是堆疊在一起。我只想堆疊順序。休息前兩個 FS 和 HRRS 我正試圖讓它像這樣。

而第三個保持堆積條形圖。請建議是否有任何其他技術或解決方案。
uj5u.com熱心網友回復:
不確定是否要完全切換繪圖庫,但這可能與 plotly 接近:
import pandas as pd
import plotly.express as px
df = pd.DataFrame({
'Categories': ["Two Instances", "Two Instances", "Two Instances"],
'X': ["FS", "HRRS", "Sequential"],
'Y': [146, 82, 80],
'Y1': [157, 160, 162]
})
dfFirstTwo = df.head(n=2)
dfThird = df.tail(n=1)
labels={'Y':'T1','Y1':'T2'}
fig = px.bar(dfThird, x='X',y=['Y','Y1'],labels={'Y':'T1','Y1':'T2'})
fig = fig.for_each_trace(lambda t: t.update(name=labels[t.name],
legendgroup = labels[t.name],
))
fig = fig.update_layout(
legend_title=""
)
fig.show()

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Bar(
x=dfFirstTwo.X,
y=dfFirstTwo.Y,
name="T1",
error_y=dict(
type='data',
array=(dfFirstTwo.Y)
)
))
fig.add_trace(go.Bar(
x=dfFirstTwo.X,
y=dfFirstTwo.Y1,
name="T2",
error_y=dict(
type='data',
array=(dfFirstTwo.Y1)
)
))
fig.update_layout(
title="Plot Title",
xaxis_title="X Axis Title",
yaxis_title="X Axis Title",
legend_title="",
)

uj5u.com熱心網友回復:
這是我用 seaborn 庫解決的問題。
我創建了兩個帶有一些虛擬值的單獨資料集,因此我可以完美地重疊它們。這是一種解決方法,或者可能是唯一的方法。更多的解決方案或完美的代碼將受到社區的贊賞。這對我有用。
data = {'Value': [146, 157, 82, 160, 0, 0],
'Name': ['T1', 'T2',
'T1', 'T2',
'T1', 'T2'],
'Param': ['FS', 'FS',
'HRRS', 'HRRS',
'Sequential', 'Sequential']}
df = pd.DataFrame(data)
df = df.sort_values(['Name', 'Param'])
df1 = pd.DataFrame({
'X': ["FS", "HRRS", "Sequential"],
'Y': [0, 0, 80],
'Y1': [0, 0, 162]
})
plt.figure()
sns.barplot(x='Param', y='Value', data=df, hue='Name', palette='CMRmap_r')
sns.barplot(x=df1["X"], y=df1["Y1"], color='#ca8d24', label="T3", ax=None)
sns.barplot(x=df1["X"], y=df1["Y"], color='#783c88', label="T3", ax=None)
plt.show()
有了這個,我將不同型別的條形圖組合成一個。

uj5u.com熱心網友回復:
一種方法是使用補丁 - 創建分組條形圖。然后,僅對于順序條,移動它們以使它們堆疊。這將允許您在一個圖中同時擁有兩種型別。
df = pd.DataFrame({'Categories': ["Two Instances", "Two Instances", "Two Instances"],
'X': ["FS", "HRRS", "Sequential"],'Y': [146, 82, 80],'Y1': [157, 160, 162]})
fig, ax = plt.subplots(figsize=(10,5))
df.plot(x="X", kind='bar', ax=ax, color=['tab:blue', 'tab:orange'])
ax.patches[2].set_x(ax.patches[2].get_x() 0.125) ## Move Seq-Y bar to right
ax.patches[5].set_x(ax.patches[5].get_x()-0.125) ## Move Seq-Y1 bar to left
ax.patches[5].set_y(ax.patches[2].get_height()) ## Move Seq-Y1 bar up
ax.set_ylim(0, ax.patches[2].get_height() ax.patches[5].get_height() 20) ##Adjust y-axis to show the taller Seq bar
plt.legend(labels=['T2', 'T1'], bbox_to_anchor=(1, 1.02), loc='lower right', borderaxespad=0, )
plt.show()
陰謀

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515947.html
