1

I have a plotly line plot and would like to assign specific colors to each line. For example, TSLA = 'blue', AAPL = 'red', etc. This is because I will create multiple scatterplots with various combinations of the entities and would like the color to be consistent.

import plotly.express as px
fig = px.line(df_pivot,  x='Range', y='Mentions', color='Term')
fig.show()

Graph shown in image

graph

Tom McLean
  • 5,583
  • 1
  • 11
  • 36
amc
  • 13
  • 2

1 Answers1

1

Use a dictionary for the colors with the parameter color_discrete_map. For example:

import plotly.express as px
q = pd.DataFrame([[1,2,1],[4,5,2],[2,6,2],[3,8,1],[5,1,1]], columns=["Range", "Mentions", 'Term'])
colors_dict = {1:"black", 2:"orange"}

fig = px.line(q,  x='Range', y='Mentions',color='Term', color_discrete_map=colors_dict)
fig.show()
Udi
  • 172
  • 1
  • 6