【问题标题】:Using Facet_col with Plotly Express Scatter_Polar charts将 Facet_col 与 Plotly Express Scatter_Polar 图一起使用
【发布时间】:2021-08-18 16:02:11
【问题描述】:

使用 px.scatter_polar 时是否可以使用 facet_col 或 facet_row 选项? 我试过了,但得到“TypeError: scatter_polar() got an unexpected keyword argument ‘facet_col’”.

import plotly.express as px
import pandas as pd

df=pd.DataFrame({'WD': {0: 'N', 1: 'N', 2: 'N', 3: 'N', 4: 'N', 5: 'N', 6: 'NNE', 7: 'NNE', 8: 'NNE', 9: 'NNE', 10: 'NNE', 11: 'NNE'}, 'WS': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 1, 7: 2, 8: 3, 9: 4, 10: 5, 11: 6}, 'Lines': {0: 0, 1: 3, 2: 5, 3: 12, 4: 1, 5: 0, 6: 0, 7: 1, 8: 4, 9: 5, 10: 2, 11: 0}, 'Site': {0: 'EQ21301', 1: 'EQ21309', 2: 'EQ21301', 3: 'EQ21306', 4: 'EQ21301', 5: 'EQ21301', 6: 'EQ21301', 7: 'EQ21301', 8: 'EQ21306', 9: 'EQ21306', 10: 'EQ21306', 11: 'EQ21309'}})
fig = px.scatter_polar(df, r="WS", theta="WD",size='Lines',facet_col='Site',
                       color='WS',color_discrete_sequence=px.colors.sequential.YlOrRd,template='plotly_dark') 


fig.show()

我知道我可以使用 make_subplots 创建它,但我认为这种方法可能会更好,这意味着我不必在每次站点数量增加时添加额外的代码。

【问题讨论】:

  • @WilH 任何有兴趣回答您的问题的人都必须复制您的文件并将其存储在与您指定的文件名对应的文件夹中,以便重现您的场景。有一些方法可以让每个人都更轻松,同时增加更快获得有用答案的机会。 Here is one way
  • 似乎facet_col 不在 ScatterPolar Charts 的可接受参数列表中,如此处 geeksforgeeks.org/… 所示。似乎这里唯一的选择是使用make_subplots

标签: python-3.x plotly scatter-plot polar-coordinates


【解决方案1】:
  • 根本不是能力...
  • 使用plotly express生成图形作为获取参数的基础
  • make_subplots() 每个网站
  • trace 复制所需参数
  • 布局复制所需参数
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
from plotly.subplots import make_subplots

df=pd.DataFrame({'WD': {0: 'N', 1: 'N', 2: 'N', 3: 'N', 4: 'N', 5: 'N', 6: 'NNE', 7: 'NNE', 8: 'NNE', 9: 'NNE', 10: 'NNE', 11: 'NNE'}, 'WS': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 1, 7: 2, 8: 3, 9: 4, 10: 5, 11: 6}, 'Lines': {0: 0, 1: 3, 2: 5, 3: 12, 4: 1, 5: 0, 6: 0, 7: 1, 8: 4, 9: 5, 10: 2, 11: 0}, 'Site': {0: 'EQ21301', 1: 'EQ21309', 2: 'EQ21301', 3: 'EQ21306', 4: 'EQ21301', 5: 'EQ21301', 6: 'EQ21301', 7: 'EQ21301', 8: 'EQ21306', 9: 'EQ21306', 10: 'EQ21306', 11: 'EQ21309'}})
# create px figure to copy formatting from
fig = px.scatter_polar(df, r="WS", theta="WD",size='Lines',
                       color='WS',color_discrete_sequence=px.colors.sequential.YlOrRd,template='plotly_dark') 


# make sub-plots for all "Site"
spfig = make_subplots(
    cols=len(df["Site"].unique()),
    specs=[[{"type": "polar"} for s in df["Site"].unique()]],
    subplot_titles=df["Site"].unique()
)
# use base go capability and copy wanted parameters from px trace
for c, site in enumerate(df["Site"].unique()):
    dft = df.loc[df["Site"].eq(site)]
    spfig.add_trace(
        go.Scatterpolar(
            {
                **fig.to_dict()["data"][0],
                **{
                    "r": dft["WS"],
                    "theta": dft["WD"],
                    "name": site,
                    "marker": {
                        **fig.to_dict()["data"][0]["marker"],
                        **{"size": dft["Lines"], "color": dft["WS"]},
                    },
                },
            },
        ),
        row=1,
        col=c + 1,
    )

# finally copy across layout parameters
spfig = spfig.update_layout({ **fig.to_dict()["layout"], **spfig.to_dict()["layout"]})
spfig.layout.template = fig.layout.template
for axis in ["polar","polar2","polar3"]:
    spfig.layout[axis]["angularaxis"] = fig.layout.polar["angularaxis"]
    
# and we're done...
spfig

【讨论】:

    猜你喜欢
    • 2022-06-11
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 2011-06-10
    • 1970-01-01
    • 2018-08-05
    相关资源
    最近更新 更多