1 回答

TA貢獻1816條經驗 獲得超4個贊
目前,在 HoloViews (1.13) 中,條形圖不可能有超過 2 個分類變量。
另請參閱此 github 問題:
https ://github.com/holoviz/holoviews/issues/2878
但是,您可以執行以下解決方法:
訣竅是在關鍵字中放置一個x分類變量,一個分類變量,以及其他分類變量關鍵字by中的變量。groupby
import pandas as pd
import hvplot.pandas
# create sample data
df = pd.DataFrame({
'Type': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
'Fiscal Period': ['2019-01', '2019-01', '2019-02', '2019-02', '2019-01', '2019-01', '2019-02', '2019-02'],
'Request Type': ['S', 'D', 'S', 'D', 'S', 'D', 'S', 'D'],
'values': range(1, 9),
})
# create a separate barchart per Type
layout = df.hvplot.bar(
x='Fiscal Period',
y='values',
by='Request Type',
groupby='Type',
stacked=True,
cmap='Category20',
legend='top_left',
width=400,
xlabel='',
).layout()
# make plots nicer so they look more like a clustered barchart
plotA = layout['A'].opts(title='Type: A')
plotB = layout['B'].opts(show_legend=False, yaxis=None, ylabel='', title='Type: B')
# add separate plots together again
(plotA + plotB).opts(title='Showing the counts per Fiscal Period, Request Type and Type')
結果圖:作為獎勵,此代碼將為您提供與上述相同的結果:
https://i.stack.imgur.com/fcl7z.png
def create_subplot(type_selected):
plot = df[df['Type'] == type_selected].hvplot.bar(
x='Fiscal Period',
y='values',
by='Request Type',
stacked=True,
cmap='Category20',
label='Type: ' + type_selected,
legend='top_left',
width=400,
xlabel='',
ylabel='',
)
return plot
plotA = create_subplot('A')
plotB = create_subplot('B').opts(show_legend=False, yaxis=None)
(plotA + plotB).opts(title='Showing the counts per Fiscal Period, Request Type and Type')
添加回答
舉報