Plotly - how to change x axis labels in python?

I draw a horizontal glass histogram and I want each element on the x axis to reflect a datetime value instead of an integer. I draw the values ​​in seconds. How to change tick marks of x-axis to reflect date-time? 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)
+4
source share
2 answers

In your layout you need to specify the type that will be the category, as well as specify the values ​​of the category and array. Also, in order for your graphic diagrams to display correctly, they must be arrays. The code below does what you would like to do, given the fact that your datetime is a 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)
+3
source

(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'
        )
    )
)
+1

Source: https://habr.com/ru/post/1614957/


All Articles