简体   繁体   中英

Create a line chart with datetime on x-axis and number of records on y-axis

I am trying to create a line chart with only the month-year part from the datetime data on the x-axis and the number of records for each month on the y.

So far, I have copied the two columns that I need from the original dataframe into a new one and changed the format of the datetime column:

line_chart =frame[['index', 'Start Time and Date']].copy()
line_chart['Start Time and Date']=line_chart['Start Time and Date'].dt.to_period('M')

Next, I tried to use plotly to create the chart:

import plotly.express as px
fig = px.line(line_chart, x='Start Time and Date', y='index')
fig.show()

But I get this error: Object of type Period is not JSON serializable

I also tried plotting with matplotlib :

x=line_chart['Start Time and Date']
y=line_chart['index']
plt.plot(x,y)
plt.gcf().autofmt_xdate()

plt.show()

But again error: `view limit minimum 0.0 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units

Here are some screenshots of the data: Frame Frame1

I would really appreciate some help! Thank you!

If you leave the format of the 'Start Time and Date' column unchanged to the default pandas datetime format and instead update the format of the x-axis tick labels your code should work.

import pandas as pd

frame = pd.DataFrame({'Start Time and Date': ['2013-07-01 00:00:00', '2013-07-01 00:00:02', '2013-07-01 00:01:04',
                                              '2013-07-01 00:01:06', '2013-07-01 00:01:10', '2013-08-01 00:00:00',
                                              '2013-08-01 00:00:02', '2013-09-01 00:01:04', '2013-09-01 00:01:06',
                                              '2013-10-01 00:01:10', '2013-10-01 00:02:10', '2013-11-01 00:03:10',
                                              '2013-12-01 00:03:10', '2013-12-02 00:04:10', '2013-12-03 00:05:10'],
                      'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]})

# Number of records per month
line_chart = frame.copy()
line_chart.index = pd.DatetimeIndex(line_chart['Start Time and Date'])
line_chart = pd.DataFrame(line_chart.resample('M')['index'].count())
line_chart.reset_index(inplace=True)

# Plotly
import plotly.express as px

fig = px.line(line_chart, x='Start Time and Date', y='index')
fig.update_layout(xaxis=dict(tickformat='%m-%Y'))
fig.show()

# Matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

x = line_chart['Start Time and Date']
y = line_chart['index']

fig, ax = plt.subplots(figsize=(10, 6))
plt.plot(x, y)
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%Y'))
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM