【发布时间】:2021-11-01 10:54:28
【问题描述】:
我想绘制一个时间序列区域图,其中正 (>= 0) 值以一种颜色填充,负 (
举个例子:
import pandas as pd
import numpy as np
import plotly.express as px
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv').assign(
PnL = lambda x: x['AAPL.Close'] - 100
)
px.area(
data_frame = df,
x = 'Date',
y = 'PnL',
width = 500,
height = 300
)
我希望将 PnL 低于 0 的部分填充为红色。
这就是我尝试过的:
import pandas as pd
import numpy as np
import plotly.express as px
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv').assign(
PnL = lambda x: x['AAPL.Close'] - 100
)
df['sign'] = np.where(df['PnL'] >= 0, 'positive', 'negative')
px.area(
data_frame = df,
x = 'Date',
y = 'PnL',
color = 'sign',
color_discrete_map = {
'positive': 'steelblue',
'negative': 'crimson'
},
width = 500,
height = 300
)
但这给了我:
这不是我想要的。最好的方法是什么?
【问题讨论】:
-
您能否详细解释一下与您要查找的内容有何不同?
标签: python plotly plotly-express