我有一個名為 case_deaths 的資料框:
week daily_case_totals daily_death_totals
0 1 2.0 0.0
1 2 12.0 0.0
2 3 12.0 0.0
3 4 2.0 0.0
4 5 573.0 6.0
5 6 3134.0 12.0
6 7 3398.0 32.0
7 8 992.0 25.0
.
.
.
這段代碼生成到 Seaborn 圖表:
fig, axes = plt.subplots(2, 1, figsize=(11, 10))
for name, ax in zip(['daily_case_totals', 'daily_death_totals'], axes):
sns.barplot(data=cases_deaths, x='week', y=name, ax=ax, color = 'red')
圖表如下所示:

但我希望頂部為藍色,底部為紅色。不知道該怎么做,我試過將顏色串列傳遞給 for 回圈中的顏色引數,但產生了錯誤。
uj5u.com熱心網友回復:
只需zip為顏色添加一個可迭代的:
import seaborn as sns
fig, axes = plt.subplots(2, 1, figsize=(11, 10))
for name, color, ax in zip(('daily_case_totals', 'daily_death_totals'),
('blue', 'red'),
axes):
sns.barplot(data=cases_deaths, x='week', y=name, ax=ax, color=color)

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/374550.html
標籤:Python matplotlib 海生 条形图
上一篇:熊貓欄如何標記所需的值
下一篇:在顏色條中劃分不同的顏色
