Change Line Colour with Plotly Express
Asked Answered
S

6

22

I have a plotly express figure:

fig = px.line(data, x="DateTime", y="Gold", title="Gold Prices")

I want to change some details, like so

fig.update_layout(
    line_color="#0000ff",  # ValueError: Invalid property specified for object of type plotly.graph_objs.Layout: 'line'
    line=dict(
        color='rgb(204, 204, 204)',
        width=5
    ),  # Bad property path: line
)

But both attempts (trying solutions I researched on here) failed, with the errors given in the comments.

I have also tried fig = px.line(data, x="DateTime", y="Gold", title="Gold Prices", template="ggplot2", color_discrete_map={"Gold": "green"}) to no avail.

How do I make this work please?

Sublet answered 2/9, 2021 at 7:39 Comment(0)
N
20

Try to use .update_traces() with plotly.express instead of .update_layout():

fig.update_traces(line_color='#0000ff', line_width=5)
Nashville answered 6/9, 2021 at 13:43 Comment(2)
Does this work when there are multiple lines please? If so, how to set them?Sublet
Works for multiple lines but than all lines seem to have the same color, wouldn't take a color list as an input (I had just one line, but a row split). Used the indexing system from r-beginners below.Ursas
S
10

plotly.express

If you want to use plotly.express, add the following settings.

import plotly.express as px

df = px.data.stocks()
fig = px.line(df, x='date', y="GOOG", title='Ticker:GOOG')

fig['data'][0]['line']['color']='rgb(204, 204, 204)'
fig['data'][0]['line']['width']=5

fig.show()

plotly.graph_objects

If you are using plotly.graph_objects, you can set it in go.Scatter().

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

df = px.data.stocks()
fig = go.Figure(data=go.Scatter(x=df['date'], y=df['GOOG'], mode='lines', line_color='rgb(204, 204, 204)', line_width=5))
fig.update_layout(title='Ticker:GOOG')

fig.show()

enter image description here

Sublease answered 6/9, 2021 at 12:35 Comment(1)
The output of each is the same, so they are one and the same.Sublease
H
4
fig = px.line(data, x="DateTime", y="Gold", title="Gold Prices", color_discrete_map={"DateTime": "blue", "Gold": "green"})
Hyperaemia answered 25/4, 2023 at 8:52 Comment(1)
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Biblio
C
3

Have you tried simply.

fig.add_scattergl(x=xs, y=df.y, line={'color': 'black'})

Source

Calv answered 10/9, 2021 at 15:34 Comment(0)
L
2

Try using the "set_color" function like so:

fig.set_color('b')

or

fig.set_color('g')

where g and b can also be r for the RGB color scheme. then fig.show()

Levenson answered 10/9, 2021 at 15:19 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Annunciator
Doesn't work for me: AttributeError: 'Figure' object has no attribute 'set_color'Sublet
U
1

This worked for me quite well:


fig = px.line(
    data, 
    x="DateTime", 
    y="Gold", 
    title="Gold Prices",
    color_discrete_sequence=["rgb(204, 204, 204)"] # This parameter do the trick.
)

It also works in using something like: color_discrete_sequence=["black"]

Unblown answered 5/10, 2023 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.