Plotly - 如何在python中更改x轴标签?

时间:2015-11-08 18:04:51

标签: python plotly

我正在绘制水平堆积条形图,我希望x轴上的每个项目都反映日期时间值而不是整数。我正以秒为单位绘制值。如何更改x轴刻度标签以反映日期时间?谢谢!

import plotly.plotly as plt
import plotly.graph_objs as gph

data = [
    ('task 1', 300),
    ('task 2', 1200),
    ('task 3', 500)
]
traces = []
for (key, val) in data:
    traces += [gph.Bar(
        x=val,
        y=1,
        name=key,
        orientation='h',
        )]

layout = gph.Layout(barmode='stack')
fig = gph.Figure(data=traces, layout=layout)
plt.iplot(fig)

2 个答案:

答案 0 :(得分:3)

在布局中,您需要指定类别以及类别顺序和数组值。另外,为了使条形图正确打印,它们需要是数组。下面的代码似乎按照您的意愿行事 - 假设您的日期时间是值。

import plotly.plotly as plt
import plotly.graph_objs as gph

data = [
    ('task 1', 300),
    ('task 2', 1200),
    ('task 3', 500)
]
vals = []
traces = []
for (key, val) in data:
    vals.append(val)
    traces.append(gph.Bar(
        x=[val],
        y=[1],
        name=key,
        ))

layout = gph.Layout(xaxis=dict(categoryorder='array', categoryarray=vals, type="category"))
fig = gph.Figure(data=traces, layout=layout)
plt.iplot(fig)

答案 1 :(得分:1)

以下是一个示例(docs

layout = gph.Layout(
    title='Plot Title',
    xaxis=dict(
        title='x Axis',
        titlefont=dict(
            family='Courier New, monospace',
            size=18,
            color='#7f7f7f'
        )
    ),
    yaxis=dict(
        title='y Axis',
        titlefont=dict(
            family='Courier New, monospace',
            size=18,
            color='#7f7f7f'
        )
    )
)