【发布时间】:2019-04-14 10:46:34
【问题描述】:
我在时间线上绘制营销数据,其中只有发送的时间(而不是日期)是相关的,因为该列仅包含时间数据(从 csv 导入)
它显示各种线图(意大利面条图)但是,当我想将标签添加到 x 轴时,我会收到
RuntimeError: Locator 试图从 30282.0 到 76878.0 生成 4473217 个刻度:超过 Locator.MAXTICKS
我有这个测试文件的 140 行数据,时间在 9:05 到 20:55 之间,我的代码应该每 15 分钟得到一个滴答声。
蟒蛇:3.7.1.final.0 蟒蛇位: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()
所以我认为这个问题与我声明蜱虫的方式有关,试图在这里找到相关的帖子,但没有一个解决我的问题。谁能指出我正确的方向?提前致谢。
【问题讨论】:
-
我认为这可能是this question 的“重复”。要在此处重复最佳答案,您不应使用
interval指定 15 分钟间隔(因为这仅指示哪个刻度获得标签),而应使用MinuteLocator(byminute=[0,15,30,45], interval = 1) -
@Asmus 它更改了虚拟集上的刻度,但在实际集上运行它时我收到相同的错误消息。由于脚本处理部分数据,我认为它仍然与要绘制的数据量有关,或者..?
-
您的代码似乎试图在 0083 年 11 月 28 日至 0211 年 6 月 27 日之间创建微小的滴答声。这确实很奇怪,但如果没有 @,就无法找出为什么它会这样做987654322@,即问题中独立的、可运行的代码。
-
扩展@ImportanceOfBeingErnest 所说的内容,您能告诉我们
df_Campaign2['time sent'].min()和df_Campaign2['time sent'].max()的结果是什么吗? -
看起来虚拟集在转换“发送时间”值时没有使用 .time(),所以看起来像虚拟集中的最小值 = datetime.datetime(1900, 1 , 1, 9, 5) 而实际中的最小值 = datetime.time(9, 45)。删除 .time() 函数时,它工作正常 - 我找不到它有这种可能的行为。我花了一些时间并获得了帮助,如何从 datetime 对象中删除 1900,1,1 par,这似乎实际上导致了问题?
标签: python-3.x matplotlib