Python Plotly Express:如何有条件地填充面积图?
Python Plotly Express: How to conditionally fill an area plot?
我想绘制时间序列面积图,其中正 (>= 0) 值用一种颜色填充,负 (< 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
)
但这给了我:
这不是我要找的。执行此操作的最佳方法是什么?
这是我在有耐心的时候能做的最好的事情:
import plotly.graph_objects as go
mask = df['PnL'] >= 0
df['PnL_above'] = np.where(mask, df['PnL'], 0)
df['PnL_below'] = np.where(mask, 0, df['PnL'])
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['Date'], y=df['PnL_above'], fill='tozeroy'))
fig.add_trace(go.Scatter(x=df['Date'], y=df['PnL_below'], fill='tozeroy'))
结果:
显然不理想,但可以帮助您完成大部分工作。两条轨迹相交处有一些轻微的伪影,显然当值为零时线条颜色仍然可见。
在两条轨迹上加上mode='none'
,就可以去掉线条,只渲染填充区域:
我想绘制时间序列面积图,其中正 (>= 0) 值用一种颜色填充,负 (< 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
)
但这给了我:
这不是我要找的。执行此操作的最佳方法是什么?
这是我在有耐心的时候能做的最好的事情:
import plotly.graph_objects as go
mask = df['PnL'] >= 0
df['PnL_above'] = np.where(mask, df['PnL'], 0)
df['PnL_below'] = np.where(mask, 0, df['PnL'])
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['Date'], y=df['PnL_above'], fill='tozeroy'))
fig.add_trace(go.Scatter(x=df['Date'], y=df['PnL_below'], fill='tozeroy'))
结果:
显然不理想,但可以帮助您完成大部分工作。两条轨迹相交处有一些轻微的伪影,显然当值为零时线条颜色仍然可见。
在两条轨迹上加上mode='none'
,就可以去掉线条,只渲染填充区域: