gpt4 book ai didi

python - 绘制时间太接近

转载 作者:行者123 更新时间:2023-12-01 00:28:39 26 4
gpt4 key购买 nike

我的情节将日期时间挤得太近了。

我有以下代码:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as md
import matplotlib.dates as mdates


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)

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()

情节如下:

enter image description here

我想绘制数据的日期。然而,有些日期时间太接近,我无法找到一种方法将它们均匀地分开。我怎样才能做到这一点?

最佳答案

试试这个:

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)

enter image description here

关于python - 绘制时间太接近,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com