我有如下數據框: month start end0 2018-1 1000 01 2018-2 1100 0.12 2018-3 1400 0.33 2018-4 700 -0.5如果我想用線繪制“開始”列,用條形圖繪制“結束”列(x 軸應該是月份),并且只在同一個圖中使用 pandas 繪圖函數,我應該怎么做?+ 對于條形圖,最好在圖表的末尾有一條黑色水平線 = 0,并且條形圖采用顏色編碼,使得正回報為綠色,負回報為紅色。我開始嘗試ax = df.plot(figsize=(10,5), x='month', y='start')df.plot(figsize=(10,5), x='month', y='end', kind='bar', ax=ax)axpass但它看起來不是我想要的。提前致謝!
2 回答

PIPIONE
TA貢獻1829條經驗 獲得超9個贊
IIUC,你需要這樣的東西。如何有條件地為條形著色已從此處獲取。我從剪貼板中獲取了您在問題中提供的數據
import pandas as pd
df = pd.read_clipboard()
df['positive'] = df['end'] > 0
ax = df.plot(figsize=(10,5), x='month', y='start')
ax1 = df.plot(figsize=(10,5), x='month', y='end', kind='bar', color=df.positive.map({True: 'g', False: 'r'}),
ax=ax, secondary_y=True)
ax1.plot([0, len(df['end'])], [0, 0], color='black')

瀟湘沐
TA貢獻1816條經驗 獲得超6個贊
ax=df.plot(figsize=(10,5),x="month",y="End",kind="bar")
df.plot(x='month', y='Start',kind="line",ax=ax)
對我來說很好。
添加回答
舉報
0/150
提交
取消