3

Trying to use plotly js to make a plot of dates, but for some reason it is showing time and date.

How can I remove time from this plot? Thanks!

x = ["2023-02-13", "2023-02-14"];
y = [3, 4];
var trace1 = {
    type: "scatter",
    mode: "lines",
    name: '',
    x: x,
    y: y,
    line: {color: '#17BECF'}
}
var layout = {
    xaxis: {
        type: 'date',
    },
    title: '',
};
var data = [trace1];
Plotly.newPlot('plotDiv', data, layout);

enter image description here

Derek O
  • 16,770
  • 4
  • 24
  • 43
Frank
  • 952
  • 1
  • 9
  • 23

1 Answers1

3

We can specify the tickformat in the xaxis of the layout with tickformat: '%b %d, %Y' to avoid including the time. And to avoid having the same date repeat on multiple ticks for the same day, we can also set the distance between ticks to be 1 day using dtick: 86400000.0 (which will be in units of seconds according to the documentation).

The codepen can be viewed here.

var layout = {
    xaxis: {
        type: 'date',
        tickformat: '%b %d, %Y',
        dtick: 86400000.0
    },
    title: '',
};

enter image description here

Derek O
  • 16,770
  • 4
  • 24
  • 43