Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set the line color in Python Plotly?
Python Plotly provides several methods to customize line colors in graphs. In this tutorial, we'll explore how to set line colors using plotly.express and the update_traces() method.
Plotly Express contains many methods to customize charts and render them in HTML format. The update_traces() method with the line_color parameter is the primary way to set line colors after creating a plot.
Basic Line Color Setting
Here's a complete example showing how to create a line plot and set its color ?
import plotly.express as px
import pandas as pd
# Create sample data
data = {'marks': [12, 13, 11, 14, 15], 'average': [5, 6, 5, 7, 8]}
df = pd.DataFrame(data)
# Create line plot
fig = px.line(df, x='marks', y='average', title='Sample Line Plot')
# Set line color to purple
fig.update_traces(line_color='purple')
fig.show()
Multiple Line Colors
When working with multiple lines, you can set colors for each line individually ?
import plotly.express as px
import pandas as pd
# Create data with multiple series
data = {
'x': [1, 2, 3, 4, 5],
'y1': [10, 15, 13, 17, 20],
'y2': [8, 12, 10, 14, 18]
}
df = pd.DataFrame(data)
# Create multiple lines
fig = px.line()
fig.add_scatter(x=df['x'], y=df['y1'], mode='lines', name='Series 1', line=dict(color='red'))
fig.add_scatter(x=df['x'], y=df['y2'], mode='lines', name='Series 2', line=dict(color='blue'))
fig.show()
Color Options
Plotly accepts various color formats ?
import plotly.express as px
import pandas as pd
data = {'x': [1, 2, 3, 4], 'y': [10, 15, 13, 17]}
df = pd.DataFrame(data)
# Using named colors
fig1 = px.line(df, x='x', y='y')
fig1.update_traces(line_color='crimson')
# Using hex colors
fig2 = px.line(df, x='x', y='y')
fig2.update_traces(line_color='#FF6B6B')
# Using RGB values
fig3 = px.line(df, x='x', y='y')
fig3.update_traces(line_color='rgb(255, 107, 107)')
print("Colors set successfully using different formats")
Colors set successfully using different formats
Line Color with Additional Properties
You can combine line color with other styling properties ?
import plotly.express as px
import pandas as pd
data = {'month': ['Jan', 'Feb', 'Mar', 'Apr'], 'sales': [100, 150, 120, 180]}
df = pd.DataFrame(data)
fig = px.line(df, x='month', y='sales', title='Monthly Sales')
# Set multiple line properties
fig.update_traces(
line_color='green',
line_width=3,
line_dash='dash'
)
fig.show()
Color Methods Comparison
| Method | When to Use | Example |
|---|---|---|
update_traces() |
Single line or all lines | fig.update_traces(line_color='red') |
line=dict() |
Individual traces | line=dict(color='blue') |
color parameter |
Categorical coloring | px.line(color='category') |
Conclusion
Use update_traces(line_color='color') for simple line color changes. For multiple lines, use the line=dict(color='color') parameter when adding traces. Plotly supports named colors, hex codes, and RGB values for maximum flexibility.
