Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to plot just month and day on x axis? (Ignore year)

I am trying to plot timeseries data and would like the x axis to just be Month and Day. Plotly requires format to be yyyy-mm-dd, but I have daily averages from a data set over a few years, so I just want to plot mm-dd on the x axis. When I send a datetime that is just mm-dd it assumes the mm to be the year. Can I get it to bypass the year and only take mm-dd?

df_en_ave1.index = df_en_ave1.index.strftime('%m-%d') #convert my index to month and day datetime

trace1=go.Scatter(x=df_en_ave1.index, y=df_en_ave1.evap) #need to bypass year in date here somehow

data = [trace1]

plotly.offline.iplot(data)
like image 480
spencerchad Avatar asked Sep 14 '18 22:09

spencerchad


People also ask

How do you set the X axis range 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 I change the date format in Plotly?

To customize date format, from 'Axes' under 'Style' menu choose 'Tick Labels' submenu. Next, select the axis you wish to modify, and then set 'advanced(d3-time-format)' for 'Label Format' attribute. A text box will appear where you can enter a custom time format.


2 Answers

please try following way:

dcc.Graph(figure={
'data': [{
    'x': ['2015-01-01', '2015-01-10 15:30:12', '2015-04-01'],
    'y': [2, 1, 5]
}],
'layout': {
    'xaxis': {
        'tickformat': '%d/%m'
    }
}})

other formats

%a - abbreviated weekday name.*
%A - full weekday name.*
%b - abbreviated month name.*
%B - full month name.*
%c - the locale's date and time, such as %x, %X.*
%d - zero-padded day of the month as a decimal number [01,31].
%e - space-padded day of the month as a decimal number [ 1,31]; equivalent to %_d.
%f - microseconds as a decimal number [000000, 999999].
%H - hour (24-hour clock) as a decimal number [00,23].
%I - hour (12-hour clock) as a decimal number [01,12].
%j - day of the year as a decimal number [001,366].
%m - month as a decimal number [01,12].
%M - minute as a decimal number [00,59].
%L - milliseconds as a decimal number [000, 999].
%p - either AM or PM.*
%Q - milliseconds since UNIX epoch.
%s - seconds since UNIX epoch.
%S - second as a decimal number [00,61].
%u - Monday-based (ISO 8601) weekday as a decimal number [1,7].
%U - Sunday-based week of the year as a decimal number [00,53].
%V - ISO 8601 week of the year as a decimal number [01, 53].
%w - Sunday-based weekday as a decimal number [0,6].
%W - Monday-based week of the year as a decimal number [00,53].
%x - the locale's date, such as %-m/%-d/%Y.*
%X - the locale's time, such as %-I:%M:%S %p.*
%y - year without century as a decimal number [00,99].
%Y - year with century as a decimal number.
%Z - time zone offset, such as -0700, -07:00, -07, or Z.
%{n}f for fractional seconds with n digits. For example, 2016-10-13 09:15:23.456 with tickformat %H~%M~%S.%2f would display 09~15~23.46*
%% - a literal percent sign (%).

thanks to https://community.plot.ly/t/how-to-make-the-messy-date-ticks-organized/7477/3

like image 132
Cristopher Fuentealba Avatar answered Oct 11 '22 16:10

Cristopher Fuentealba


The answer:

fig.update_layout(xaxis=dict(tickformat="%d-%m"))

Example plot:

enter image description here

Complete code:

# imports
import numpy as np
import pandas as pd
import plotly.express as px

# sample time series data

nperiods=200

np.random.seed(123)
df = pd.DataFrame(np.random.randint(-10,12,size=(nperiods, 4)), columns=list('ABCD'))
datelist = pd.date_range(pd.datetime(2020, 1, 1).strftime('%Y-%m-%d'), periods=nperiods).tolist()
df['dates'] = datelist 
df = df.set_index(['dates'])
df.index = pd.to_datetime(df.index)
df.iloc[0]=0
df=df.cumsum().reset_index()

# melt data to provide the data structure mentioned earlier
dfm=pd.melt(df, id_vars=['dates'], value_vars=df.columns[1:])
dfm.set_index('dates')
dfm.head()

# plotly
fig = px.line(dfm, x="dates", y="value", color='variable')
fig.update_layout(xaxis=dict(tickformat="%d-%m"))
fig.show()
like image 35
vestland Avatar answered Oct 11 '22 15:10

vestland