How can I manually set an upper limit for legend/data bounds in plotly express python?

408 views Asked by At

I am making several scatter plots. Some of the scatter plots display data from 0-12, some go from 0-15 etc. The legend will show 0-max for each graph, but it makes them hard to compare, as 12 on one is the max and it is only about 80% of max on the other (so one will show blue, the other green) I want to set it to where the same color is shown for the same number each time, ie blue is always the max I pass in.

def makeplot(ptitle,data,htmlname,variable,outpath,lim=0):
    fig = px.scatter(data, x = 'var1', y='var2', color = variable, color_continous_scale=['red','orange',yellow',green','blue'], title=ptitle)
1

There are 1 answers

0
Redox On

@Jacob - To set each value (0 to red, 12 to green, etc.), you can set the number of entries you are providing to the color_continous_scale as below. But do be careful to ensure that the number of entries are less than or equal to the number of colors. Also, as you are providing a discrete set of colors to the scatter plot, assuming that the values of var1 and var2 will be within this set.

def makeplot(ptitle,data,htmlname,variable,outpath,lim=0):
    colors = ['red','orange','yellow','green','blue']
    fig = px.scatter(data, x = 'var1', y='var2', color = variable, color_continuous_scale=colors[0:len(data)], title=ptitle)

df1 = pd.DataFrame({'var1':[0,4,8,12], 'var2':[0,4,8,12]})
df2 = pd.DataFrame({'var1':[0,4,8,12,16], 'var2':[0,4,8,12,16]})
variable1=[0,4,8,12]
variable2=[0,4,8,12,16]

makeplot("4wala", df1,"dummy",variable1,"dummy2")
makeplot("5wala", df2,"dummy",variable2,"dummy2")

enter image description here

enter image description here