我正在时间轴上绘制活动数据,其中只有发送的时间(而不是日期)才有意义,该列仅包含时间数据(从csv导入)
它显示各种折线图(意大利面条图),但是,当我要将标签添加到x轴时,会收到
RuntimeError:定位器尝试从30282.0到76878.0生成4473217滴答:超过Locator.MAXTICKS
此测试文件有140行数据,时间在9:05到20:55之间,我的代码应该每15分钟响一次。
python:3.7.1.final.0 python位:64 操作系统:Windows 操作系统版本:10
熊猫:0.23.4
matplotlib:3.0.2
我的实际代码如下:
import pandas
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
file_name = r'''C:\\Users\\A_B_testing.csv'''
df1 = pandas.read_csv(file_name, encoding='utf-8')
df_Campaign1 = df1[df1['DataSource ID'].str.contains('Campaign1')==True]
Campaign1_times = df_Campaign1['time sent'].tolist()
Campaign1_revenue = df_Campaign1['EstValue/sms'].tolist()
Campaign1_times = [datetime.strptime(slot,"%H:%M").time() for slot in Campaign1_times]
df_Campaign2 = df1[df1['DataSource ID'].str.contains('Campaign2')==True]
Campaign2_times = df_Campaign2['time sent'].tolist()
Campaign2_revenue = df_Campaign2['EstValue/sms'].tolist()
Campaign2_times = [datetime.strptime(slot,"%H:%M").time() for slot in Campaign2_times]
fig, ax = plt.subplots(1, 1, figsize=(16, 8))
xlocator = mdates.MinuteLocator(byminute=None, interval=15) # tick every 15 minutes
xformatter = mdates.DateFormatter('%H:%M')
ax.xaxis.set_major_locator(xlocator)
ax.xaxis.set_major_formatter(xformatter)
ax.minorticks_off()
plt.grid(True)
plt.plot(Campaign1_times, Campaign1_revenue, c = 'g', linewidth = 1)
plt.plot(Campaign2_times, Campaign2_revenue, c = 'y', linewidth = 2)
plt.show()
我厌倦了减少要绘制的值的数量,并且在虚拟集上按如下所示工作良好:
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import HourLocator, MinuteLocator, DateFormatter
from datetime import datetime
fig, ax = plt.subplots(1, figsize=(16, 6))
xlocator = MinuteLocator(interval=15)
xformatter = DateFormatter('%H:%M')
ax.xaxis.set_major_locator(xlocator)
ax.xaxis.set_major_formatter(xformatter)
ax.minorticks_off()
plt.grid(True, )
xvalues = ['9:05', '10:35' ,'12:05' ,'12:35', '13:05']
xvalues = [datetime.strptime(slot,"%H:%M") for slot in xvalues]
yvalues = [2.2, 2.4, 1.7, 3, 2]
zvalues = [3.2, 1.4, 1.8, 2.7, 2.2]
plt.plot(xvalues, yvalues, c = 'g')
plt.plot(xvalues, zvalues, c = 'b')
plt.show()
因此,我认为该问题与我声明滴答声的方式有关,试图在此处找到相关的帖子,但没有一个解决我的问题。谁能指出我正确的方向?预先感谢。
答案 0 :(得分:0)
我有一个类似的问题,该问题通过在x轴上使用datetime
对象而不是time
对象来解决。
类似地,在问题代码中,使用完整的datetime
而非仅使用time
应该可以解决此问题。
替换:
[datetime.strptime(slot,"%H:%M").time() for slot in ...
作者:
[datetime.strptime(slot,"<full date format>") for slot in