我的情节将日期时间过于紧密地捆绑在一起。我有以下代码:import pandas as pdimport matplotlib.pyplot as pltimport matplotlib.dates as mdimport matplotlib.dates as mdatesdata = {"Date":['2019-2-25 14:24:04', '2019-2-25 14:29:02', '2019-2-20 17:02:14', '2019-3-25 08:12:54', '2019-3-25 12:32:32'], "Quiz":[1,2,2,3,4], "Score":[10,3,7,4,9]}df = pd.DataFrame.from_dict(data)df.Date = pd.to_datetime(df.Date) fig = plt.figure(figsize = (11,9))ax = fig.add_subplot(111)ax.set_xticks(df.Date)ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))ax.set_xlabel("Date")ax.set_ylabel("Score")ax.plot_date(df.Date, df.Score)fig.autofmt_xdate(rotation=45)plt.show()这是情节:我想绘制我的数据的日期。但是,某些日期时间太靠近了,我无法找到一种方法将它们均匀地分开。我怎样才能做到这一点?
1 回答
芜湖不芜
TA贡献1796条经验 获得超7个赞
尝试这个:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as md
import matplotlib.dates as mdates
import datetime as dt
data = {"Date":['2019-2-25 14:24:04', '2019-2-25 14:29:02', '2019-2-20 17:02:14',
'2019-3-25 08:12:54', '2019-3-25 12:32:32'],
"Quiz":[1,2,2,3,4],
"Score":[10,3,7,4,9]}
df = pd.DataFrame.from_dict(data)
xs = df.Date.values
fig, ax = plt.subplots(1)
ax.set_xlabel("Date")
ax.set_ylabel("Score")
ax.plot_date(xs, df.Score)
fig.autofmt_xdate(rotation=45)
plt.show()
主要区别是删除格式化业务并设置 xs = df.Date.values,然后让图表自己处理格式化fig.autofmt_xdate(rotation=45)。
添加回答
举报
0/150
提交
取消