2017-04-20 48 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()