11

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. I am plotting the values in terms of seconds. How can I change the x axis tick labels to reflect a datetime? 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)

2 Answers 2

6

Here is an example (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'
        )
    )
)
Sign up to request clarification or add additional context in comments.

Comments

4

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.