简体   繁体   中英

Plotly: How to set title color for scatter traces when not using plotly express?

When I do a scatter plot like this:

fig2 = go.Figure()

for num in range(len(dy)):
    data = dz[0:,num]
    fig2.add_trace(go.Scatter(x=dx, y=data, name=str(dy[num]), mode='lines'))

How do I set the color coding title, like when doing color= in plotly.express.line ?

在此处输入图片说明

You can edit font color, size and family through fig.update(legend_title=dict(font=dict(color='Blue'))) :

在此处输入图片说明

Complete code:

import plotly.express as px
import plotly.graph_objects as go

df = px.data.gapminder().query("continent=='Oceania'")

countries = df['country'].unique()
countries

fig2 = go.Figure()
for country in countries:
    df2 = df[df['country']==country]
    fig2.add_traces(go.Scatter(x=df2['year'], y=df2['lifeExp'], name = country,
                              mode='lines',
                              ))

fig2.update_xaxes(title='Year')
fig2.update_yaxes(title='LiefExp')

fig2.update_layout(legend_title=dict(text='Country',
                                     font=dict(family="sans-serif",
                                     size = 18,
                                     color='blue')))


fig2.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM