简体   繁体   English

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

[英]Plotly - how to change x axis labels in python?

I am plotting a horizontal stacked bar chart, and I want each item on the x axis to reflect a datetime value instead of an integer. 我正在绘制一个水平堆积条形图,我希望x轴上的每个项目都反映日期时间值而不是整数。 I am plotting the values in terms of seconds. 我正以秒为单位绘制值。 How can I change the x axis tick labels to reflect a datetime? 如何更改x轴刻度标签以反映日期时间? Thanks! 谢谢!

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)

In your layout you need to specify the type to be category as well as specify the categoryorder and array values. 在布局中,您需要指定类别以及类别顺序和数组值。 Also in order to get your bar charts to print properly they need to be arrays. 另外,为了使条形图正确打印,它们需要是数组。 The code below seems to do what you would like it to do - given the fact that your datetime is the value. 考虑到你的日期时间是值,下面的代码似乎做你想做的事情。

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)

Here is an example ( docs ) 这是一个例子( 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'
        )
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM