Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly - how to change x axis labels in python?

Tags:

python

plotly

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)
like image 524
wgillis Avatar asked Nov 08 '15 18:11

wgillis


People also ask

How do you change the X axis in Plotly?

Setting the Range of Axes Manually The visible x and y axis range can be configured manually by setting the range axis property to a list of two values, the lower and upper boundary. Here's an example of manually specifying the x and y axis range for a faceted scatter plot created with Plotly Express.

How do you add labels in Plotly?

As a general rule, there are two ways to add text labels to figures: Certain trace types, notably in the scatter family (e.g. scatter , scatter3d , scattergeo etc), support a text attribute, and can be displayed with or without markers. Standalone text annotations can be added to figures using fig.

What font does Plotly use?

The default font family spec is "Open Sans", verdana, arial, sans-serif , as listed in Python Figure Reference: layout from the Plotly documentation. The precise font selected depends on which fonts you have installed on your system. If Open Sans is available, that font will be selected.


1 Answers

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'
        )
    )
)
like image 198
mic4ael Avatar answered Sep 18 '22 15:09

mic4ael