Python 3.x 在Plotly Express散点极坐标图中使用刻面坐标

Python 3.x 在Plotly Express散点极坐标图中使用刻面坐标,python-3.x,plotly,scatter-plot,polar-coordinates,Python 3.x,Plotly,Scatter Plot,Polar Coordinates,在使用px.scatter\u polar时,是否可以使用facet\u col或facet\u row选项? 我已经尝试过了,但是得到了“TypeError:scatter\u polar()得到了一个意外的关键字参数'facet\u 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: '

在使用px.scatter\u polar时,是否可以使用facet\u col或facet\u row选项? 我已经尝试过了,但是得到了
“TypeError:scatter\u polar()得到了一个意外的关键字参数'facet\u 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_subplot
创建它,但我认为这种方法可能更好,意味着我不必每次站点数量增加时都添加额外的代码。

  • 简单地说,这不是一种能力
  • 使用plotly express生成图形作为获取参数的基础
  • 为每个站点制作子地块()
  • 跟踪跨所需参数复制
  • 布局跨所需参数复制

@will任何有兴趣回答您的问题的人都必须复制您的答案,并将其存储在与您指定的姓名对应的文件夹中,以便重现您的情景。有很多方法可以让每个人都更容易得到答案,同时也增加了更快得到有用答案的机会。似乎
facet\u col
不在散极图的可接受参数列表中,如图所示。似乎这里唯一的选择就是使用
make_子图
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