time on xaxis in plotly - python

I have my x-axis values in this format : ['May 23 2018 06:31:52 GMT', 'May 23 2018 06:32:02 GMT', 'May 23 2018 06:32:12 GMT', 'May 23 2018 06:32:22 GMT', 'May 23 2018 06:32:32 GMT']
and corresponding values for the y-axis which are some numbers.
But when I am plotting these using plotly , x-axis show only part of the date (May 23 2018) for each point. Time for each point is not shown.
I tried setting up tickformat also in layout, but it does not seems to work.
layout = go.Layout(
title=field+ "_its diff_value chart",
xaxis = dict(
tickformat = '%b %d %Y %H:%M:%S'
)
)
any help is appreciated.
This is the screenshot of the graph made.

Try converting your x-values to datetime objects
Then tell plotly to use a fixed tick distance
import random
import datetime
import plotly
plotly.offline.init_notebook_mode()
x = [datetime.datetime.now()]
for d in range(100):
x.append(x[0] + datetime.timedelta(d))
y = [random.random() for _ in x]
scatter = plotly.graph_objs.Scatter(x=x, y=y)
layout = plotly.graph_objs.Layout(xaxis={'type': 'date',
'tick0': x[0],
'tickmode': 'linear',
'dtick': 86400000.0 * 14}) # 14 days
fig = plotly.graph_objs.Figure(data=[scatter], layout=layout)
plotly.offline.iplot(fig)

To skip inconsistent time series, add this before plotting the plotly chart
fig.update_xaxes(
rangebreaks=[
dict(bounds=['2018-05-23 06:31:52','2018-05-23 06:32:02']),
dict(bounds=['2018-05-23 06:32:02','2018-05-23 06:32:12']),
dict(bounds=['2018-05-23 06:32:12','2018-05-23 06:32:22']),
dict(bounds=['2018-05-23 06:32:22','2018-05-23 06:32:32'])
]
)

Related

AttributeError: 'NoneType' object has no attribute 'dpi_scale_trans'

Having checked through StackOverflow for a possible solution to the above error that I encountered, I found some solutions which were not able to solve this particular issue.
The following image shows the error encountered and the undesirable figure obtained while trying to plot multiple axes on a figure:
This is an image of the expected output:
Also, the data I am working with can be found at: https://drive.google.com/file/d/1ZxD3BwpJfEVMWPoxu-aom_wXawMfSywk/view?usp=sharing
Below is the set of codes that I have scripted, and I seem to have hit a roadblock. I would appreciate any help that will guide me to achieve the expected output #image2.
import matplotlib.dates as md
# Date already in DateTime index
dk1 = data.loc["1991":"2000"] # selects all rows from 1991 - 2000
dk2 = data.loc["2001":"2010"] # selects all rows from 2001 - 2010
dk3 = data.loc["2011":"2020"] # selects all rows from 2011 - 2020
plt.rcParams['font.size'] = 18
fig = plt.figure(figsize=(20,15)) # Create a figure for the plot
# Add three axes to the plots
ax1 = plt.subplot(111)
ax2 = plt.subplot(212)
ax3 = plt.subplot(313)
# Plot the data
ax1.plot(dk1.TX, label = 'Max temp')
ax2.plot(dk2.TX, label = 'Max temp')
ax3.plot(dk3.TX, label = 'Max temp')
# Set the title
ax1.set_title("Station: Ikeja, 1991~2000, tmax\n")
ax2.set_title("Station: Ikeja, 2001~2010, tmax\n")
ax3.set_title("Station: Ikeja, 2011~2020, tmax\n")
ax1.legend() # Plot the legend for first axes
ax2.legend() # Plot the legend for second axes
ax3.legend() # Plot the legend for third axes
# Set the x- and y-axis label
ax1.set_ylabel('Temperature (°C)\n') # Set the Y-Axis label
ax1.set_xlabel('\nYear') # Set the X-Axis label
ax2.set_ylabel('Temperature (°C)\n') # Set the Y-Axis label
ax2.set_xlabel('\nYear') # Set the X-Axis label
ax3.set_ylabel('Temperature (°C)\n') # Set the Y-Axis label
ax3.set_xlabel('\nYear') # Set the X-Axis label
# formatted X axis in Year
ax1.xaxis.set_major_locator(md.YearLocator(month = 1, day = 1)) # formatted X axis in Year
ax1.xaxis.set_major_formatter(md.DateFormatter('%Y')) # set the date format to the year shortname
ax2.xaxis.set_major_locator(md.YearLocator(month = 1, day = 1)) # formatted X axis in Year
ax2.xaxis.set_major_formatter(md.DateFormatter('%Y')) # set the date format to the year shortname
ax3.xaxis.set_major_locator(md.YearLocator(month = 1, day = 1)) # formatted X axis in Year
ax3.xaxis.set_major_formatter(md.DateFormatter('%Y')) # set the date format to the year shortname
# Set the limits (range) of the X-Axis
ax1.set_xlim([pd.to_datetime('1991 1 1', format = format),
pd.to_datetime('2000 12 31', format = format)])
ax2.set_xlim([pd.to_datetime('2001 1 1', format = format),
pd.to_datetime('2010 12 31', format = format)])
ax3.set_xlim([pd.to_datetime('2010 1 1', format = format),
pd.to_datetime('2020 12 31', format = format)])
plt.show()
The error is due to the argument passed to matplotlib.pyplot.subplot: since you want three plot on three different rows (same column), then you should use:
ax1 = plt.subplot(3, 1, 1)
ax2 = plt.subplot(3, 1, 2)
ax3 = plt.subplot(3, 1, 3)
In any case, your code need some changes in order to achieve the results you want.
First of all it is wise to convert 'Time' column from str type to datetime:
data['time'] = pd.to_datetime(data['time'], format = '%m/%d/%Y')
Then, since you need to filter data based on year, you should create a column with the year:
data['year'] = data['time'].dt.year
At this point you can filter your data based on the 'year' column:
dk1 = data[(data['year'] >= 1991) & (data['year'] <= 2000)] # selects all rows from 1991 - 2000
dk2 = data[(data['year'] >= 2001) & (data['year'] <= 2010)] # selects all rows from 2001 - 2010
dk3 = data[(data['year'] >= 2011) & (data['year'] <= 2020)] # selects all rows from 2011 - 2020
As already mentioned, you should create the subplots in the proper way:
ax1 = plt.subplot(3, 1, 1)
ax2 = plt.subplot(3, 1, 2)
ax3 = plt.subplot(3, 1, 3)
Finally, pay attention to the set_xlim code: format is a special keyword in python:
ax1.set_xlim([pd.to_datetime('1991 1 1', format = '%Y %m %d'),
pd.to_datetime('2000 12 31', format = '%Y %m %d')])
ax2.set_xlim([pd.to_datetime('2001 1 1', format = '%Y %m %d'),
pd.to_datetime('2010 12 31', format = '%Y %m %d')])
ax3.set_xlim([pd.to_datetime('2010 1 1', format = '%Y %m %d'),
pd.to_datetime('2020 12 31', format = '%Y %m %d')])
Complete code
import matplotlib.dates as md
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv(r'data/max_temp.csv')
data['time'] = pd.to_datetime(data['time'], format = '%m/%d/%Y')
data['year'] = data['time'].dt.year
# # Date already in DateTime index
dk1 = data[(data['year'] >= 1991) & (data['year'] <= 2000)] # selects all rows from 1991 - 2000
dk2 = data[(data['year'] >= 2001) & (data['year'] <= 2010)] # selects all rows from 2001 - 2010
dk3 = data[(data['year'] >= 2011) & (data['year'] <= 2020)] # selects all rows from 2011 - 2020
plt.rcParams['font.size'] = 18
fig = plt.figure(figsize=(20,15)) # Create a figure for the plot
# Add three axes to the plots
ax1 = plt.subplot(3, 1, 1)
ax2 = plt.subplot(3, 1, 2)
ax3 = plt.subplot(3, 1, 3)
# Plot the data
ax1.plot(dk1.time, dk1.TX, label = 'Max temp')
ax2.plot(dk2.time, dk2.TX, label = 'Max temp')
ax3.plot(dk3.time, dk3.TX, label = 'Max temp')
# Set the title
ax1.set_title("Station: Ikeja, 1991~2000, tmax\n")
ax2.set_title("Station: Ikeja, 2001~2010, tmax\n")
ax3.set_title("Station: Ikeja, 2011~2020, tmax\n")
ax1.legend() # Plot the legend for first axes
ax2.legend() # Plot the legend for second axes
ax3.legend() # Plot the legend for third axes
# Set the x- and y-axis label
ax1.set_ylabel('Temperature (°C)\n') # Set the Y-Axis label
ax1.set_xlabel('\nYear') # Set the X-Axis label
ax2.set_ylabel('Temperature (°C)\n') # Set the Y-Axis label
ax2.set_xlabel('\nYear') # Set the X-Axis label
ax3.set_ylabel('Temperature (°C)\n') # Set the Y-Axis label
ax3.set_xlabel('\nYear') # Set the X-Axis label
# formatted X axis in Year
ax1.xaxis.set_major_locator(md.YearLocator(month = 1, day = 1)) # formatted X axis in Year
ax1.xaxis.set_major_formatter(md.DateFormatter('%Y')) # set the date format to the year shortname
ax2.xaxis.set_major_locator(md.YearLocator(month = 1, day = 1)) # formatted X axis in Year
ax2.xaxis.set_major_formatter(md.DateFormatter('%Y')) # set the date format to the year shortname
ax3.xaxis.set_major_locator(md.YearLocator(month = 1, day = 1)) # formatted X axis in Year
ax3.xaxis.set_major_formatter(md.DateFormatter('%Y')) # set the date format to the year shortname
# Set the limits (range) of the X-Axis
ax1.set_xlim([pd.to_datetime('1991 1 1', format = '%Y %m %d'),
pd.to_datetime('2000 12 31', format = '%Y %m %d')])
ax2.set_xlim([pd.to_datetime('2001 1 1', format = '%Y %m %d'),
pd.to_datetime('2010 12 31', format = '%Y %m %d')])
ax3.set_xlim([pd.to_datetime('2010 1 1', format = '%Y %m %d'),
pd.to_datetime('2020 12 31', format = '%Y %m %d')])
plt.show()

Plotly: using fig.update_xaxes showes wrong month

I am looking for a solution to show the x_axis correct. the date 2021-01-31 is displayed as "Feb 2021". i would like to show it as "Jan 2021". thanks for help!
sdate = date(2021,1,31)
edate = date(2021,8,30)
date_range = pd.date_range(sdate,edate-timedelta(days=1),freq='m')
df_test = pd.DataFrame({ 'Datum': date_range})
df_test['values'] = 10
fig = px.line(df_test, x=df_test['Datum'], y=df_test['values'])
fig.layout = go.Layout(yaxis=dict(tickformat=".0%"))
fig.update_xaxes(dtick="M1", tickformat="%b %Y")
fig.update_layout(width=1485, height=1100)
plotly.io.write_image(fig, file='test_line.png', format='png')
You can force the ticks to start at 2021-01-31 by setting the starting tick to the starting date of your data sdate.
from datetime import date, timedelta
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
sdate = date(2021,1,31)
edate = date(2021,8,30)
date_range = pd.date_range(sdate,edate-timedelta(days=1),freq='m')
df_test = pd.DataFrame({ 'Datum': date_range})
df_test['values'] = 10
fig = px.line(df_test, x=df_test['Datum'], y=df_test['values'])
fig.layout = go.Layout(yaxis=dict(tickformat=".0%"))
fig.update_xaxes(dtick="M1", tickformat="%b %Y")
## set tick0 to the starting date
fig.update_layout(
xaxis=dict(tick0=sdate),
width=1485, height=1100
)
fig.show()
I should point out that this plot has the potential to be misleading as I believe most people would interpret each tickmark as starting at the beginning of the month (e.g. most people would think that the data starts on 2021-01-01) if you don't specify the day in your tickformat, but that is up to you depending on what you want to show on your chart.
If you instead you change the tickformat by rewriting the line fig.update_xaxes(dtick="M1", tickformat="%b %d %Y") then you get the following plot:

How to edit x-axis ticks in plotly python

I have made a plot using plotly express line. Now I want to edit the x axis ticks, so that they are 1st day of every month, meaning I would get 1 Jan 1 Feb 1 Mar 1 Apr. How can I achieve that? I was trying with
fig = px.line(df, x='date',y='value',color='transportation')
fig.update_layout(
xaxis_tickformat = '%d %b',
xaxis=dict(
tickmode = 'array',
tickvals = ['2020-02-01'],
ticktext = ['1 Feb'])
But tickvals and ticktext do not change.

visualize two columns in the same data set

I am trying to group and sort four columns, count values and chart them in the same bar graph to see the trend how the count has changed.
Year Month Bl_year Month
2018 Jan 2019 Jan
2018 Feb 2018 Mar
2018 Dec 2020 Dec
2019 Apr 2019 Sep
2020 Nov 2020 Dec
2019 Sep 2018 Jan
I tried to group and sort first and counting values first by the year and then next by the month.
df_Activity_count = df.sort_values(['year','month'],ascending = True).groupby('month')
df_Activity_count_BL = df.sort_values(['BL year','BL month'],ascending = True).groupby('BL month')
Now I am trying to compare these two in the same bar. Can someone please help.
Try to pass ax to your plot command:
df_Activity_count = df.sort_values(['year','month'],ascending = True).groupby('month')
df_Activity_count_BL = df.sort_values(['BL year','BL month'],ascending = True).groupby('BL month')
ax = df_Activity_count.years.value_counts().unstack(0).plot.bar()
df_Activity_count_BL['BL year'].value_counts().unstack(0).plot.bar(ax=ax)
Since you tagged matplotlib, I will chip in a solution using pyplot
import matplotlib.pyplot as plt
# Create an axis object
fig, ax = plt.subplots()
# Define dataframes
df_Activity_count = df.sort_values(['year','month'],ascending = True).groupby('month')
df_Activity_count_BL = df.sort_values(['BL year','BL month'],ascending = True).groupby('BL month')
# Plot using the axis object ax defined above
df_Activity_count['year'].value_counts().unstack(0).plot.bar(ax=ax)
df_Activity_count_BL['BL year'].value_counts().unstack(0).plot.bar(ax=ax)

pandas day of week axis labels

I am plotting a pandas series that spans one week. My code:
rng = pd.date_range('1/6/2014',periods=169,freq='H')
graph = pd.Series(shared_index, index=rng[:168])
graph.plot(shared_index)
Which displays 7 x-axis labels:
[06 Jan 2014, 07, 08, 09, 10, 11, 12]
But I want:
[Mon, Tue, Wed, Thu, Fri, Sat, Sun]
What do I specify in code to change axis labels?
Thanks!
perhaps you can manually fix the tick labels:
rng = pd.date_range('1/6/2014',periods=169,freq='H')
graph = pd.Series(np.random.randn(168), index=rng[:168])
ax = graph.plot()
weekday_map= {0:'MON', 1:'TUE', 2:'WED', 3:'THU',
4:'FRI', 5:'SAT', 6:'SUN'}
xs = sorted(ax.get_xticks(minor='both'))
wd = graph.index[xs - xs[0]].map(pd.Timestamp.weekday)
ax.set_xticks(xs)
ax.set_xticks([], minor=True)
ax.set_xticklabels([weekday_map[d] for d in wd])

Categories