python - go.Histogram2dContour 从 plotly 显示不同的图表类型
问题描述
我正在尝试使用带有下拉菜单的 plotly 的 Histogram2dContour 绘图函数在烧瓶应用程序中显示 2d 密度图。我使用此处的基本示例制作的等高线图。相反,我得到一个连接的散点图。图上的点是正确的。
我从下面的按钮创建函数中的 args 中删除了可见参数。我还尝试更改 plots 函数中的参数,但没有运气,
def kde_chart(self, chart_type, data_columns_list, df):
'''
Creates 2d kde denisty plot with pair combinations of columns from dataframes that can be
selected from a dropdown within the plotly visualization.
:param chart_type: string KDE
:param data_columns_list: data columns from dataframe in database
:param df: dataframe used in database
:return:
'''
if chart_type == 'KDE':
# create pairs of columns with combinations
kde_columns = [column for column in data_columns_list if df[column].dtype == 'float']
kde_columns_combinations = combinations(kde_columns, 2)
kde_columns = [pair for pair in kde_columns_combinations]
# create traces
traces = [self._kde_chart_maker(pair, df) for pair in kde_columns]
# create updatemenus
updatemenus = list([dict(
active=-1,
buttons=[self._kde_updatemenu_button_maker(pair, traces[i])
for i, pair in enumerate(kde_columns)])])
# create plot
kde_plot = dict(data=[traces],
layout=dict(
title='KDE plot',
showlegend=False,
autosize=False,
width=800,
height=550,
updatemenus=updatemenus,
hovermode='closest'
)
)
graphJSON = json.dumps(kde_plot, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
def _kde_chart_maker(self, data_column_pair, df):
# create contours
trace = go.Histogram2dContour(
x=df[data_column_pair[0]],
y=df[data_column_pair[1]],
#name='density',
ncontours=20,
colorscale='Hot',
reversescale=True,
showscale=True
)
return trace
def _kde_updatemenu_button_maker(self, kde_pair, trace_selected):
button = dict(label=kde_pair[0] + '/' + kde_pair[1],
method='update',
args=[dict(
x=[trace_selected['x']],
y=[trace_selected['y']],
title=kde_pair[0] + '/' + kde_pair[1])])
return button
未显示任何错误消息。
解决方案
没关系,我找到了解决方案。我看到了错误,然后重构了代码以适应输入数据的修改形式。
推荐阅读
- github - 如何将不同的 GitHub 包安装到我的网站
- python - My program works but is very slow, it also slows down as it runs
- r - 设置包含边缘的绘图的白色背景(ggExtra + ggplot2)
- html - 浏览器不显示图像
- python - i+ =1 在 for 循环中生成语法错误
- ruby-on-rails - Ruby on Rails 测试和控制台都以 cache_control 错误退出
- artifactory - Jcenter inclsuion:版本控制
返回404,之前工作过 - postgresql - /bookmarks 处的 PG::SyntaxError - 我无法弄清楚 SQL 查询错误的原因
- typescript - 打字稿枚举 Object.values() 返回值
- apache-spark - 有没有办法将多列添加到根据不同列和/或不同持续时间的移动平均值计算的数据框中