绘制python刻面热图

发布于 2025-01-30 19:34:08 字数 648 浏览 4 评论 0原文

我正在使用的示例。在Python中使用Seaborn进行刻面热图。结果看起来像这样:

”在此处输入图像描述”

我想对Plotly Express做同样的事情,并尝试使用此启动器代码:

import plotly.express as px
df = px.data.medals_wide(indexed=True)
fig = px.imshow(df)
fig.show()

我的数据也位于pd.dataframe中重要的是,我向这些组表明,热图和地图的X/Y轴分组。

您如何扩展px.imshow示例以按照上面的海洋示例来创建一个小组的热图?

I'm using the example from this SO Q&A to use seaborn for facetted heatmaps in python. The result looks like this:

enter image description here

I'd like to do the same thing with plotly express and have tried with this starter code:

import plotly.express as px
df = px.data.medals_wide(indexed=True)
fig = px.imshow(df)
fig.show()

My data is also in a pd.DataFrame and it's important I show the groups the heatmaps are grouped by as well as the x/y-axis of the maps.

How do you extend the px.imshow example to create a facetted heatmap by group like the seaborn example above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光与爱终年不遇 2025-02-06 19:34:08

样本数据是从参考的答复中获取的,以回答问题。如果是列数据,则可以将其作为数据来尺寸,但是结果不能与示例数据中的分类变量一起使用分类变量作为萃取条件。您可以通过在热图中使用图对象作为子图绘制它,可以通过在数据提取结果的数据框架中指定xy轴来创建xy轴。

import numpy as np
import pandas as pd
import plotly.express

# Generate a set of sample data
np.random.seed(0)
indices = pd.MultiIndex.from_product((range(5), range(5), range(5)), names=('label0', 'label1', 'label2'))
data = pd.DataFrame(np.random.uniform(0, 100, size=len(indices)), index=indices, columns=('value',)).reset_index()

import plotly.graph_objects as go
from plotly.subplots import make_subplots

titles = ['label0='+ str(x) for x in range(5)]
fig = make_subplots(rows=1, cols=len(data['label0'].unique()),
                    shared_yaxes=True,
                    subplot_titles = tuple(titles))

for i in data['label0'].unique():
    df = data[data['label0'] == i]
    fig.add_trace(go.Heatmap(z=df.value, x=df.label1, y=df.label2), row=1, col=i+1)
    fig.update_traces(showscale=False)
    fig.update_xaxes(dtick=[0,1,2,3,4])
    fig.update_xaxes(title_text='label1', row=1, col=i+1)
    fig.update_yaxes(title_text='label2', row=1, col=1)

fig.show()

The sample data is taken from the referenced responses to answer the question. express, as data, can be subplotted if it is column data, but the results cannot be used with a categorical variable as the extraction condition with a different categorical variable, as in the sample data. You can draw it if it is as a subplot using a graph object in A heat map can be created by specifying the xy-axis in the data frame of the result of data extraction by category variable.

import numpy as np
import pandas as pd
import plotly.express

# Generate a set of sample data
np.random.seed(0)
indices = pd.MultiIndex.from_product((range(5), range(5), range(5)), names=('label0', 'label1', 'label2'))
data = pd.DataFrame(np.random.uniform(0, 100, size=len(indices)), index=indices, columns=('value',)).reset_index()

import plotly.graph_objects as go
from plotly.subplots import make_subplots

titles = ['label0='+ str(x) for x in range(5)]
fig = make_subplots(rows=1, cols=len(data['label0'].unique()),
                    shared_yaxes=True,
                    subplot_titles = tuple(titles))

for i in data['label0'].unique():
    df = data[data['label0'] == i]
    fig.add_trace(go.Heatmap(z=df.value, x=df.label1, y=df.label2), row=1, col=i+1)
    fig.update_traces(showscale=False)
    fig.update_xaxes(dtick=[0,1,2,3,4])
    fig.update_xaxes(title_text='label1', row=1, col=i+1)
    fig.update_yaxes(title_text='label2', row=1, col=1)

fig.show()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文