Search code examples
pythonplotlyscatter-plotplotly-express

Plotly Express Scatter Resizing Dots


i am creating a bee swarm graph for local windspeed data. I want to make my dot sizes bigger, as the smallest are not visible enough.

Strangely enough, my list comprehension in resize is not working. One size is f.e. 10000 but stays at the same volume compared to the sizes before resizing.

I didnt see another post adressing this issue. Thy for helping. o/

data_wspd = my_data.loc[:, ["time","wspd"]]
print(data_wspd)
wspd_grouped = data_wspd.groupby("wspd")

df = {
    "wspd": [],
    "wspd_count": [],
     "wspd_size" :[]
}

for name, group in wspd_grouped:
    df["wspd"].append(name)
    df["wspd_count"].append(len(group))
df["wspd_size"] = [count*1000 for count in df["wspd_count"]]

fig_wspd = px.scatter(df,
                 x="wspd",
                 y="wspd_count",
                 size="wspd_size",
                 color="wspd",
                 hover_name="wspd",

                 )
#fig_wspd.update_yaxes(visible=False)

fig_wspd.show()
fig_wspd.write_html("wspd_beeswarm.html")

Example from the real code.


Solution

  • This is occurring because plotly.express markers are sized relative to the other markers.

    There is an argument called size_max which is set to 20 by default (see the documentation here). So the observation with the largest wspd_size will have a marker size of 20, and if you multiply that column by 1000, that observation will still have the largest wspd_size value, so it will have a marker of size 20, and all of the smaller markers will remain the same size because the ratios between marker sizes in that column haven't changed.

    I think the easiest solution is to pass the argument size_max=20*{scaling_factor} to make all of the markers larger.

    Try the following:

    size_max_default = 20
    scaling_factor = 4
    fig_wspd = px.scatter(df,
                     x="wspd",
                     y="wspd_count",
                     size="wspd_size",
                     size_max=size_max_default*scaling_factor,
                     color="wspd",
                     hover_name="wspd",
    
                     )
    

    I tried this with some sample data and you can see the markers increase in size before and after using the size_max argument:

    df = pd.DataFrame({
        "wspd":np.linspace(1,21,11),
        "wspd_count":np.linspace(1,21,11),
        "wspd_size":[1,1,1,1,2,2,5,10,20,50,50]
    })
    
    df["wspd_size"] = df["wspd_size"]*100
    
    fig_wspd = px.scatter(df,
                     x="wspd",
                     y="wspd_count",
                     size="wspd_size",
                     color="wspd",
                     hover_name="wspd",
    
                     )
    

    enter image description here

    size_max_default = 20
    scaling_factor = 4
    fig_wspd = px.scatter(df,
                     x="wspd",
                     y="wspd_count",
                     size="wspd_size",
                     size_max=size_max_default*scaling_factor,
                     color="wspd",
                     hover_name="wspd",
                     )
    

    enter image description here