2017-04-20 54 views
0

所以我有一个简单的问题。我有一个模拟一个商店生活周/月的程序。现在它需要照顾cashdesks(我不知道我是否正确地从我的语言transalted一个),因为他们有时可能会失败,并且一些专家必须到商店并修理它们。在模拟结束,程序曲线的图形看起来就像这样:Matplotlib xticks as days

enter image description here

当cashdesk已经得到了一些错误时,1.0状态/分手,然后等待技术人员来修复它,然后它返回到0,工作状态。

我或者说我的项目人员宁愿在x轴上看到别的东西。我该怎么做?我的意思是,我想它像Day 1,然后间隔,Day 2

我知道pyplot.xticks()方法,但它分配标签是在第一个参数列表中的刻度,所以后来我必须用分钟来制作2000个标签,而我只需要7个,并在上面写上几天。

+0

一天有1,440分钟。上面只显示一天半的情节吗? – dpwilson

回答

1

您可以使用matplotlib set_ticks和get_xticklabels()方法的ax,受thisthis问题的启发。

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 

minutes_in_day = 24 * 60 

test = pd.Series(np.random.binomial(1, 0.002, 7 * minutes_in_day)) 

fig, ax = plt.subplots(1) 
test.plot(ax = ax) 

start, end = ax.get_xlim() 
ax.xaxis.set_ticks(np.arange(start, end, minutes_in_day)) 

labels = ['Day\n %d'%(int(item.get_text())/minutes_in_day+ 1) for item in ax.get_xticklabels()] 
ax.set_xticklabels(labels) 

我得到类似下面的图片。

enter image description here

+2

你的一天只有60分钟吗? – ImportanceOfBeingErnest

+0

感谢您的发现。固定 – FLab

1

你是正确的轨道上plt.xticks()。试试这个:

import matplotlib.pyplot as plt 

# Generate dummy data 
x_minutes = range(1, 2001) 
y = [i*2 for i in x_minutes] 

# Convert minutes to days 
x_days = [i/1440.0 for i in x_minutes] 

# Plot the data over the newly created days list 
plt.plot(x_days, y) 

# Create labels using some string formatting 
labels = ['Day %d' % (item) for item in range(int(min(x_days)), int(max(x_days)+1))] 

# Set the tick strings 
plt.xticks(range(len(labels)), labels) 

# Show the plot 
plt.show()