Plotly:如何使用折线图的下拉列表更新Plotly数据?

如何解决Plotly:如何使用折线图的下拉列表更新Plotly数据?

我正在尝试向可绘制的折线图添加下拉菜单,以在选中时更新图数据源。我的数据有3列,看起来像这样:

1               Country  Average House Price (£)       Date
0  Northern Ireland                      47101.0 1992-04-01
1  Northern Ireland                      49911.0 1992-07-01
2  Northern Ireland                      50174.0 1992-10-01
3  Northern Ireland                      46664.0 1993-01-01
4  Northern Ireland                      48247.0 1993-04-01

“国家/地区”列包含英国的4个国家/地区,用于使用color参数为每个国家/地区创建单独的行。对于不同的住房类型,例如all_dwellingsfirst_timebuyers,我有4个不同的数据框,当尝试指定updatemenus参数时,似乎无法使用数据框格式。这是创建整个图形的代码。

lineplt = px.line(data_frame = all_dwellings,x='Date',y='Average House Price (£)',color= 'Country',hover_name='Country',color_discrete_sequence=['rgb(23,153,59)','rgb(214,163,21)','rgb(40,48,165)','rgb(210,38)']
              )
updatemenus = [
{'buttons': [
            {
            'method': 'restyle','label': 'All Dwellings','args': [{'data_frame': all_dwellings}]
            },{
            'method': 'restyle','label': 'First Time Buyers','args': [{'data_frame': first_buyers}]
            }
            ],'direction': 'down','showactive': True,}
]

lineplt = lineplt.update_layout(
            title_text='Average House Price in UK (£)',title_x=0.5,#plot_bgcolor= 'rgb(194,208,209)',xaxis_showgrid=False,yaxis_showgrid=False,hoverlabel=dict(font_size=10,bgcolor='rgb(69,95,154)',bordercolor= 'whitesmoke'),legend=dict(title='Please click legend item to remove <br>or add to plot',x=0,y=1,traceorder='normal',bgcolor='LightSteelBlue',xanchor = 'auto'),updatemenus=updatemenus
            )
lineplt = lineplt.update_traces(mode="lines",hovertemplate= 'Date = %{x} <br>' + 'Price = £%{y:.2f}')
lineplt.show()

但是我遇到以下错误:

TypeError: Object of type DataFrame is not JSON serializable

所有示例似乎都将项目转换为列表,但这似乎不适用于数据框格式。有人可以帮忙吗?如果问题不清楚,请通知我。

编辑-all_dwellings.head(20).to_dict()的输出

{'Country': {0: 'Northern Ireland    ',1: 'Northern Ireland    ',2: 'Northern Ireland    ',3: 'Northern Ireland    ',4: 'Northern Ireland    ',5: 'Northern Ireland    ',6: 'Northern Ireland    ',7: 'Northern Ireland    ',8: 'Northern Ireland    ',9: 'Northern Ireland    ',10: 'Northern Ireland    ',11: 'Northern Ireland    ',12: 'Northern Ireland    ',13: 'Northern Ireland    ',14: 'Northern Ireland    ',15: 'Northern Ireland    ',16: 'Northern Ireland    ',17: 'Northern Ireland    ',18: 'Northern Ireland    ',19: 'Northern Ireland    '},'Average House Price (£)': {0: 47101.0,1: 49911.0,2: 50174.0,3: 46664.0,4: 48247.0,5: 54891.0,6: 53773.0,7: 57594.0,8: 49804.0,9: 58586.0,10: 55154.0,11: 55413.0,12: 60239.0,13: 59094.0,14: 57131.0,15: 61849.0,16: 61951.0,17: 61595.0,18: 68705.0,19: 74869.0},'Date': {0: Timestamp('1992-04-01 00:00:00'),1: Timestamp('1992-07-01 00:00:00'),2: Timestamp('1992-10-01 00:00:00'),3: Timestamp('1993-01-01 00:00:00'),4: Timestamp('1993-04-01 00:00:00'),5: Timestamp('1993-07-01 00:00:00'),6: Timestamp('1993-10-01 00:00:00'),7: Timestamp('1994-01-01 00:00:00'),8: Timestamp('1994-04-01 00:00:00'),9: Timestamp('1994-07-01 00:00:00'),10: Timestamp('1994-10-01 00:00:00'),11: Timestamp('1995-01-01 00:00:00'),12: Timestamp('1995-04-01 00:00:00'),13: Timestamp('1995-07-01 00:00:00'),14: Timestamp('1995-10-01 00:00:00'),15: Timestamp('1996-01-01 00:00:00'),16: Timestamp('1996-04-01 00:00:00'),17: Timestamp('1996-07-01 00:00:00'),18: Timestamp('1996-10-01 00:00:00'),19: Timestamp('1997-01-01 00:00:00')}}

first_buyers的输出

{'Country': {0: 'Northern Ireland    ','Average House Price (£)': {0: 29280.0,1: 32690.0,2: 29053.0,3: 30241.0,4: 31032.0,5: 31409.0,6: 31299.0,7: 28922.0,8: 28621.0,9: 31519.0,10: 33497.0,11: 35861.0,12: 32472.0,13: 34493.0,14: 33662.0,15: 32630.0,16: 33426.0,17: 37154.0,18: 36555.0,19: 36406.0},19: Timestamp('1997-01-01 00:00:00')}}

解决方法

我已经使用您的完整数据样本进行了初步设置,我想我已经知道了。这里的挑战是Could not find file file.ext将根据px.line参数对数据进行分组。这就使得使用下拉菜单直接参考color图的来源来编辑显示的数据变得更加困难。

但是您实际上可以为不同的数据集构建多个px.line图,然后使用那里的图正确的结构来“窃取”数据。这将为您提供以下下拉菜单选项的数字:

enter image description here

enter image description here

我有点担心第二个情节可能有点偏离,但是我使用的是您提供的日期,px.line的日期如下:

enter image description here

那么毕竟这有意义吗?

下面是完整的代码没有您的数据。我们可以讨论细节,明天再作调整。再见吧。

first_timebuyers

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-