【问题标题】:Change tick text in Plotly Surface Plot在 Plotly Surface Plot 中更改刻度文本
【发布时间】:2018-09-06 22:31:14
【问题描述】:

我创建了一个曲面图,以按一天中的小时和一周中的一天来可视化变量。情节有效,但是,我无法将默认刻度文本 (0,1,2,3...) 更改为“星期一”“星期二”等。

data = [go.Surface(z=df.values.tolist(), colorscale='Blackbody')]
layout = go.Layout(
    xaxis=dict(
        tickmode = 'Array',
        ticktext = Hours, #'Hours' is a list with 24 str elements e.g. "00:00"
        tickvals = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
    ),
    yaxis=dict(
        tickmode = 'Array',
        ticktext = Weekdays, ##'Weekdays' is a list with 7 str elements e.g. "Monday"
        tickvals = [0,1,2,3,4,5,6]
    )
)
fig = go.Figure(data=data, layout=layout)
plotly.offline.plot(fig, filename='test.html')

预期 - X 轴刻度线:("00:00", "00:01", ...),Y 轴刻度线:("Monday", "Tuesday", ...)。

实际 - X 标记:(0, 1, 2, ... 23),Y 标记:(0, 1, 2, ... 6)

我的 DataFrame 是一个简单的日期/时间矩阵,有 24 列(小时)和 7 行(工作日)

df.info()
<class 'pandas.core.frame.DataFrame'>
Index: 7 entries, Monday to Sunday
Data columns (total 24 columns):
00:00    7 non-null float64
01:00    7 non-null float64
02:00    7 non-null float64
---------------------------
21:00    7 non-null float64
22:00    7 non-null float64
23:00    7 non-null float64
dtypes: float64(24)
memory usage: 1.4+ KB

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    我缺少一个包含 xaxis 和 yaxis 的外部字典,解决方案如下:

    data = [go.Surface(z=df.values.tolist(), colorscale='Blackbody')]
    layout = go.Layout(
        scene=dict(
            xaxis=dict(
                tickmode = "Array",
                ticktext = Hours,
                tickvals = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
            ),
            yaxis=dict(
                tickmode = "Array",
                ticktext = Weekdays,
                tickvals = [0,1,2,3,4,5,6]
            )
        )
    )
    fig = go.Figure(data=data, layout=layout)
    plotly.offline.plot(fig, filename='test.html')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 2020-10-04
      • 2019-01-30
      • 1970-01-01
      相关资源
      最近更新 更多