将 Facet_col 与 Plotly Express Scatter_Polar 图表一起使用

Using Facet_col with Plotly Express Scatter_Polar charts

是否可以在使用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 创建它,但我认为这种方法可能更好,并且意味着我不必在每次站点数量增加时都添加额外的代码。

  • 根本不是能力...
  • 使用plotly express生成图形作为获取参数的基础
  • make_subplots() 每个 站点
  • trace
  • 复制想要的参数
  • layout
  • 复制想要的参数
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