【问题标题】:plotly imagesc repeat yaxis labelsplotly imagesc 重复 yaxis 标签
【发布时间】:2022-10-17 14:27:36
【问题描述】:

我有一个使用 plotly express 渲染的宽矩阵。比方说:

import plotly.express as px
data=[[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]
fig = px.imshow(data,
                labels=dict(x="Day of Week", y="Time of Day", color="Productivity"),
                x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
                y=['Morning', 'Afternoon', 'Evening']
               )
fig.update_xaxes(side="top")

fig.layout.height = 500
fig.layout.width = 500

fig.show()

为了提高可读性,我想在矩阵的右侧重复(或添加相同的)yaxis。

我试着关注this

fig.update_layout(xaxis=dict(domain=[0.3, 0.7]),
    # create 1st y axis
    yaxis=dict(
    title="yaxis1 title",),
    # create 2nd y axis
    yaxis2=dict(title="yaxis2 title", anchor="x", overlaying="y",
                side="right")
    )

但我不能让它与imshow 一起工作,因为它不接受 yaxis 参数。

任何解决方法?

【问题讨论】:

  • 但是你只有y轴的数据,第二个y轴的数据在哪里?请尝试彻底浏览您添加的链接。你会发现你应该有每个额外的 y 轴的数据。
  • 不能完全使示例与 imshow 一起使用...
  • 您将在热图上绘制什么类型的图表?您将如何在热图和其他绘图之间结合以获得 2 个 y 轴?
  • 我只想在左侧和右侧都有相同的刻度
  • 但是你右边有比例尺,你会隐藏它吗?

标签: plotly-python


【解决方案1】:

通过plotly forum找到答案:

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

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

data=[[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]

fig.add_trace(go.Heatmap(
                z=data,
                x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
                y=['Morning', 'Afternoon', 'Evening']
),secondary_y=False)
fig.add_trace(go.Heatmap(
                z=data,
                x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
                y=['Morning', 'Afternoon', 'Evening']
),secondary_y=True)

fig.update_xaxes(side="top")
fig.update_layout(xaxis_title="Day of Week", yaxis_title="Time of Day")

fig.show()

请注意,添加两次跟踪可能不是最理想的,但它确实有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-24
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    相关资源
    最近更新 更多