简体   繁体   English

Plotly 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 fe 10000 but stays at the same volume compared to the sizes before resizing.一种尺寸为 fe 10000,但与调整尺寸前的尺寸相比体积保持不变。

I didnt see another post adressing this issue.我没有看到另一个帖子解决这个问题。 Thy for helping.谢谢你的帮助。 o/ 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")

来自真实代码的示例。

This is occurring because plotly.express markers are sized relative to the other markers.发生这种情况是因为plotly.express标记的大小相对于其他标记。

There is an argument called size_max which is set to 20 by default (see the documentation here ).有一个名为size_max的参数,默认设置为 20(请参阅此处的文档)。 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.因此,具有最大wspd_size的观察将具有 20 的标记大小,如果将该列乘以 1000,该观察仍将具有最大的 wspd_size 值,因此它将具有大小为 20 的标记,以及所有较小的标记将保持相同的大小,因为该列中标记大小之间的比率没有改变。

I think the easiest solution is to pass the argument size_max=20*{scaling_factor} to make all of the markers larger.我认为最简单的解决方案是传递参数size_max=20*{scaling_factor}使所有标记变大。

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:我用一些样本数据尝试了这个,你可以看到在使用 size_max 参数之前和之后标记的大小增加了:

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",

                 )

在此处输入图像描述

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",
                 )

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM