我想将图例符号从线更改为正方形。示例代码如下:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
def save_fig(fig,pngname):
fig.write_image(pngname,format="png",width=600,height=400, scale=1)
print("[[%s]]"%pngname)
return
df = px.data.stocks()
print(df.columns)
df = df[['date','AMZN', 'AAPL']]
fig = go.Figure()
for name in df.columns:
if name == 'date': continue
trace = go.Scatter(
x = df['date'],
y = df[name],
name=name,
)
fig.add_trace(trace)
save_fig(fig,"./demo.png")
正确的做法是什么?
建立在 @PenguinBlues' answer :
fig = go.Figure()
for name, color in zip(
df.columns.difference(["date"]),
fig.layout.template.layout.colorway
):
trace = go.Scatter(
x = df["date"],
y = df[name],
name=name,
legendgroup=name,
marker=dict(color=color),
showlegend = False,
)
fig.add_trace(trace)
fig.add_shape(
name=name,
legendgroup=name,
showlegend=True,
type="rect",
line=dict(width=0),
fillcolor=color,
y0=0, y1=0,
)
fig.update_xaxes(range=[df["date"].min(), df["date"].max()])
fig.update_yaxes(range=[df.describe().loc["min"].min(),
df.describe().loc["max"].max()])
fig.show();
输出: