我试图通过x轴上的日期时间数据和y上每个月的记录数来创建仅包含月-年部分的折线图。
到目前为止,我已经将需要的两列从原始数据帧复制到一个新的列中,并更改了datetime列的格式:
X
接下来,我尝试使用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')
创建图表:
plotly
但我收到此错误:import plotly.express as px
fig = px.line(line_chart, x='Start Time and Date', y='index')
fig.show()
我还尝试使用Object of type Period is not JSON serializable
进行绘图:
matplotlib
但再次出现错误:`视图下限最小值0.0小于1,并且是无效的Matplotlib日期值。如果您将非datetime值传递给具有datetime单位的轴,通常会发生这种情况
我非常感谢您的帮助!谢谢!
答案 0 :(得分:0)
如果您将“开始时间和日期”列的格式保留为默认的熊猫日期时间格式,而是更新x轴刻度标签的格式,则代码应该可以使用。
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()