- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 plotly 和 make_subplot 绘制多个图表,我想绘制 5 个使用迭代创建的饼图环形。和 1 个条形图,其中条形图将采用 colspan = 2
eventtype_name date eventmohafaza_name number groups
watch movie 8/14/2020 loc1 25 group1
stay at home 8/13/2020 loc2 20 group2
swimming 8/13/2020 loc2 25 group4
camping 8/13/2020 loc3 20 group3
meeting 8/13/2020 loc4 40 group5
meeting 8/13/2020 loc5 20 group2
watch movie 8/14/2020 loc4 25 group1
stay at home 8/13/2020 loc1 20 group2
swimming 8/13/2020 loc5 25 group4
camping 8/13/2020 loc4 20 group3
meeting 8/13/2020 loc3 30 group5
meeting 8/13/2020 loc5 33 group2
问题是当我分配行和列并尝试使用specs参数时系统崩溃并显示以下错误。
This is the format of your plot grid:
[ (1,1) ] [ (1,2) ] [ (1,3) ]
[ (2,1) ] [ (2,2) ] [ (2,3) ]
[ (3,1) ] [ (3,2) x,y - ]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
f:\aienv\lib\site-packages\plotly\subplots.py in _set_trace_grid_reference(trace, layout, grid_ref, row, col, secondary_y)
1318 try:
-> 1319 subplot_refs = grid_ref[row - 1][col - 1]
1320 except IndexError:
IndexError: list index out of range
During handling of the above exception, another exception occurred:
Exception Traceback (most recent call last)
<ipython-input-108-831d326e87b8> in <module>
59 rotation=90),
60 row=row,
---> 61 col=col
62 )
63
f:\aienv\lib\site-packages\plotly\basedatatypes.py in add_trace(self, trace, row, col, secondary_y)
1650 rows=[row] if row is not None else None,
1651 cols=[col] if col is not None else None,
-> 1652 secondary_ys=[secondary_y] if secondary_y is not None else None,
1653 )
1654
f:\aienv\lib\site-packages\plotly\basedatatypes.py in add_traces(self, data, rows, cols, secondary_ys)
1764 if rows is not None:
1765 for trace, row, col, secondary_y in zip(data, rows, cols, secondary_ys):
-> 1766 self._set_trace_grid_position(trace, row, col, secondary_y)
1767
1768 # Make deep copy of trace data (Optimize later if needed)
f:\aienv\lib\site-packages\plotly\basedatatypes.py in _set_trace_grid_position(self, trace, row, col, secondary_y)
1847 grid_ref = self._validate_get_grid_ref()
1848 return _set_trace_grid_reference(
-> 1849 trace, self.layout, grid_ref, row, col, secondary_y
1850 )
1851
f:\aienv\lib\site-packages\plotly\subplots.py in _set_trace_grid_reference(trace, layout, grid_ref, row, col, secondary_y)
1320 except IndexError:
1321 raise Exception(
-> 1322 "The (row, col) pair sent is out of "
1323 "range. Use Figure.print_grid to view the "
1324 "subplot grid. "
Exception: The (row, col) pair sent is out of range. Use Figure.print_grid to view the subplot grid.
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import plotly.express as px
import pandas as pd
df = pd.read_excel(file)
lst = list(df.groupby('eventmohafaza_name'))
rows = 3
cols = 3
subplot_titles = [l[0] for l in lst]
specs=[
[{"type": "pie"},{"type": "pie"},{"type": "pie"}],
[{"type": "pie"},{"type": "pie"},{"type": "pie"}],
[{"type": "pie"},{"type": "bar",'colspan':2},None],
]
fig = make_subplots(
rows=rows,
cols=cols,
subplot_titles=subplot_titles,
specs=specs,
print_grid=True
)
for i, l in enumerate(lst):
row = i // cols + 1
col = i % (rows + 1) + 1
d = l[1]
fig.add_trace(go.Pie(labels=d["eventtype_name"],
values = d["number"],
hovertemplate = "%{label}: <br>Value: %{value} ",
showlegend=True,
textposition='inside',
rotation=90),
row=row,
col=col
)
group_event_groups = df.groupby([df['eventmohafaza_name'],df['eventtype_name']]).size().unstack()
for x in group_event_groups.columns:
fig.add_trace(go.Bar(name=str(x), x=group_event_groups.index, y=group_event_groups[x],showlegend=False), row=3, col=2)
fig.update_layout(height = 1000,
width = 900,
autosize = False,
title="Population by Continent",
title_x=0.5)
fig.show()
预期结果是5张饼图和一张条形图
最佳答案
示例数据新增属性数据,修改rows和cols的计算逻辑。此外,最后一个条形图现在是单个图,因为图的数量与循环数据不匹配。
lst = list(df.groupby('eventmohafaza_name'))
rows = 3
cols = 3
subplot_titles = [l[0] for l in lst]
specs=[[{"type": "pie"},{"type": "pie"},{"type": "pie"}],
[{"type": "pie"},{"type": "pie"},{"type": "pie"}],
[{"type": "pie"},{"type": "bar",'colspan':2},None]]
fig = make_subplots(
rows=rows,
cols=cols,
subplot_titles=subplot_titles,
specs=specs,
print_grid=True
)
for i, l in enumerate(lst):
row = i // cols + 1
col = (i % rows) + 1
d = l[1]
fig.add_trace(go.Pie(labels=d["eventtype_name"],
values = d["number"],
hovertemplate = "%{label}: <br>Value: %{value} ",
showlegend=True,
textposition='inside',
rotation=90),
row=row,
col=col
)
group_event_groups = df.groupby([df['eventmohafaza_name'],df['eventtype_name']]).size().unstack()
# for x in group_event_groups.columns:
fig.add_trace(go.Bar(name='camping', x=group_event_groups.index, y=group_event_groups.columns, showlegend=False), row=3, col=2)
fig.update_layout(height = 1000,
width = 900,
autosize = False,
title="Population by Continent",
title_x=0.5)
fig.show()
关于python - 如何在 plotly 和 python 中使用 make_subplot 绘制多个图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64710320/
我想更改 plotly(_express) 图中的构面标签。剧情如下: import plotly.express as px tips = px.data.tips() fig = px.scatt
我正在尝试使用 plotly.js 在 map 上绘制数据。我知道您可以通过以下方式获得一个国家/地区的 map : layout = dict( title = '',
关于 this page暗示他们有一些默认的色标,例如“Viridis”。我终其一生都找不到一个网页来记录这些命名的色标是什么。 最佳答案 问题是我是英国人并且正确拼写了颜色。色标可在 https:/
在下面的示例中,我在一个 plotly 子图中有四个箱形图。此示例中的四个箱形图中的每一个都有 3 个变量:股票、债券和现金。在每个箱线图中,我希望股票以相同的颜色(例如蓝色)显示,债券以相同的颜色(
我有一个 plotly plot,当数据发生变化时,我想删除 plot 并生成一个新 plot。为此,我这样做: $('#heatmap2').empty() 然后我重新生成我的 plotly 。但是
有许多问题和答案以一种或另一种方式涉及这个主题。有了这个贡献,我想清楚地说明为什么一个简单的方法,比如 marker = {'color' : 'red'}将适用于 plotly.graph_obje
这可能是一个非常愚蠢的问题,但是当使用 .plot() 绘制 Pandas DataFrame 时,它非常快并且会生成具有适当索引的图形。一旦我尝试将其更改为条形图,它似乎就失去了所有格式并且索引
我用 plotly (express) 生成了很多图像,并将它们以 png 格式保存在本地目录中。我想创建一个带有 plotly dash 的仪表板。我生成的图像有很多依赖关系:这就是我不想将代码包含
最近,我正在学习Plotly express和Altair/Vega-Lite进行交互式绘图。他们两个都令人印象深刻,我想知道他们的优点和缺点是什么。尤其是对于创建交互式地块,它们之间有什么大差异,何
在 plotly 中,我可以创建一个直方图,例如in this example code from the documentation : import plotly.express as px df
来自 Matlab 我正在努力弄清楚为什么以下不起作用: plot(x=rand(10),y=rand(10)) 正确生成图表。 x=rand(10) y=rand(10) plot(x,y) 产生错
我和一位同事一直在尝试设置自定义图例标签,但到目前为止都失败了。下面的代码和详细信息 - 非常感谢任何想法! 笔记本:toy example uploaded here 目标:将图例中使用的默认比率值
我正在使用 Plotly python 库生成一个带有几个 fiddle 图和几个填充散点图的图形。无论什么订单我都有个人fig.add_trace在我的代码中调用, fiddle 图总是在散点图后面
我将图表的大小配置为 Shiny 但图表之间仍有空白区域 它们在配置高度和宽度之前显示为旧区域 这是我的代码 plot1_reactive % layout(xaxis = xaxis,
我想弄清楚如何组织一个包含多个应用程序的破折号项目。所有示例都是单页应用程序,我希望将多个破折号组织为一个项目,由 gunicorn 运行(在 docker 容器内): dash-project/
我之前做了一些解决方法来在 Julia Plotly 中实现精彩的子图,但目前正在努力解决一个更复杂的问题。下面有三种方法可以完成这项工作。 draw1 完美地完成了,但不适用于我的情况,draw2
我的子图之间有很大的空间。在 matplotlib 中,有一种称为紧密布局的布局可以消除这种情况。 plotly 有没有类似的布局?我正在 iPython 笔记本中绘图,因此空间有限。请参阅下图中的空
我正在尝试获取我提前生成的 cbrewer Reds 颜色图。但是,当我尝试使用它时,我仍然得到一些默认的颜色图。我究竟做错了什么?这是 plotly :https://plot.ly/~smirno
我一直在使用 plot.ly 并希望将多个跟踪分组到图例中的同一个键。 我有显示有关特定用户的数据的子图。我想让每个键代表一个用户,而不是 user.data1、user.data2 等。 这是我现在
我有下面这张图,我想把除点和三角形以外的所有东西都去掉,意思是横纵轴上的数字和小竖线,我该怎么做? 这是图片: 这是我的代码: x0 = np.average(triangleEdges,axis=0
我是一名优秀的程序员,十分优秀!