【发布时间】:2021-09-23 14:16:35
【问题描述】:
我正在使用 python Plotly(版本 5.1.0)构建一个 Sunburst 图表。
我一直在关注这里的教程:
https://plotly.com/python/sunburst-charts/#sunburst-chart-with-a-continuous-colorscale
具体来说,我正在尝试复制底部标题为“具有连续色阶的旭日图”的最后一个示例。
当我在本地运行它时,一切正常。但是,当我尝试将其部署到我的服务器时,以下代码行会产生错误。
fig = make_subplots(1, 1, specs=[[{"type": "domain"}, {"type": "domain"}]],)
我得到以下 ValueError:
The 'specs' argument to make_subplots must be a 2D list of dictionaries with
dimensions (1 x 1).
Received value of type <class 'list'>: [[{'type': 'domain'}, {'type': 'domain'}]]
我不确定为什么会收到此错误,因为我正在使用相同数据结构的示例。在本地它工作得很好。我不确定这是否是导入问题、库冲突等。
这是我的代码。
from plotly import graph_objs as go
from plotly.tools import make_subplots
import pandas as pd
df = pd.read_csv('../sunburst_pd.csv')
levels = ['PD', 'State', 'Region']
color_columns = ['BP', 'Black']
value_column = 'BP'
def build_hierarchical_dataframe(df, levels, value_column, color_columns=None):
df_all_trees = pd.DataFrame(columns=['id', 'parent', 'value', 'color'])
for i, level in enumerate(levels):
df_tree = pd.DataFrame(columns=['id', 'parent', 'value', 'color'])
dfg = df.groupby(levels[i:]).sum()
dfg = dfg.reset_index()
df_tree['id'] = dfg[level].copy()
if i < len(levels) - 1:
df_tree['parent'] = dfg[levels[i+1]].copy()
else:
df_tree['parent'] = 'total'
df_tree['value'] = dfg[value_column]
df_tree['color'] = dfg[color_columns[0]] / dfg[color_columns[1]]
df_all_trees = df_all_trees.append(df_tree, ignore_index=True)
total = pd.Series(dict(id='total', parent='',
value=df[value_column].sum(),
color=df[color_columns[0]].sum() /
df[color_columns[1]].sum()))
df_all_trees = df_all_trees.append(total, ignore_index=True)
return df_all_trees
df_all_trees = build_hierarchical_dataframe(df, levels, value_column,
color_columns)
average_score = df['BP'].sum() / df['Black'].sum()
fig = make_subplots(1, 2, specs=[[{"type": "domain"}, {"type": "domain"}]],)
fig.add_trace(go.Sunburst(
labels=df_all_trees['id'],
parents=df_all_trees['parent'],
values=df_all_trees['value'],
branchvalues='total',
marker=dict(
colors=df_all_trees['color'],
colorscale='RdBu',
cmid=average_score),
hovertemplate='<b>%{label} </b> <br> BP: %{value}<br>
BP Population: %. {color:.6f}',
name=''
), 1, 1)
fig.add_trace(go.Sunburst(
labels=df_all_trees['id'],
parents=df_all_trees['parent'],
values=df_all_trees['value'],
branchvalues='total',
marker=dict(
colors=df_all_trees['color'],
colorscale='RdBu',
cmid=average_score),
hovertemplate='<b>%{label} </b> <br> BP: %{value}<br>
BP Population: %{color:.6f}',
maxdepth=2
), 1, 2)
fig.update_layout(margin=dict(t=10, b=10, r=10, l=10))
fig.show()
这是我的数据快照:
Region. |. State. | PD. |. BP. |. Black
South. |.Florida. |. FL. |. 3. |. 1500
North. | New York. |.NY. |. 7. |. 1275
任何帮助将不胜感激。
【问题讨论】:
-
我无法运行您的示例代码,因为显然我无权访问您的文件系统...我希望它对我来说确实失败了
fig = make_subplots(1, 1, specs=[[{"type": "domain"}, {"type": "domain"}]],),因为您要求 1 col, 1行并传递一个 1x2 囊列表... -
@RobRaymond 感谢您的回复。我相信这是问题所在,但我不太确定如何重构......
-
您是否只想要一个旭日形图?即根本不需要使用子图?
-
@RobRaymond 非常感谢您的帮助。如果我只想使用一种旭日纹,你知道我该怎么做吗?
-
知道了——使用 plotly express 会更简单,用更好的样本数据更新你的问题,我会为你提供答案
标签: python plotly data-visualization plotly-python