Skip to content
Advertisement

Plotly: How to change the format of the values for the x axis?

I need to create a graph from data with python.

I took my inspiration from various website and I’ve made this script :

JavaScript

This script allow to generate this graph : enter image description here

For the x axe, I would like to display the value like that 2020-01-01-06:00 but when I change my list like that :

JavaScript

The error is :

JavaScript

If I try that :

JavaScript

The error is :

JavaScript

Please, could you show me how to change that ?

Advertisement

Answer

The answer:

In the following code snippet I’ve replaced your dates with floats following this approach to serialize timestamps. This way you can use your dates both as input to sm.OLS and as one of a few more steps to get your dates displayed in the figure with your desired format.

The plot:

enter image description here

The details:

There are several reasons why you are not getting your desired result in your provided code snippet. First of all, none of the attempts of constuctring lists of date and time values are easily recognizable by the functions you are applying here. In date = [ '2020-01-01-06:00', '2020-01-01-12:00',...] you should remove one of the hyphens to get ['2020-01-01 06:00', '2020-01-01 12:00'...] instead. But even with a more widely recognizable list of timestamps, statsmodels will to my knowledge not accept those in sm.OLS(). And in the end, applying sensible labels to non-standard x-axis tickmarks can be (one of very few) real challenges in plotly.

Please not that the irregegular appearances of gridlines reflect the structure of your data. You’re missing observations for timestamps that end with 00-00-00 to represent a 24 hour cycle.

The code:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement