2017-09-30 62 views
0

我有一个很简单的例子,如果freq ='D',但如果freq ='T'则不会奏效。python matplotlib Bar plot适用于D freq,但不适用于T

波纹管例如:

import numpy as np 
import pandas as pd 
from matplotlib import pyplot as plt 
import time 

periods = 10 

data = [x for x in range(periods)] 
data[4:7] = np.nan, np.nan, np.nan 
idx = pd.date_range(start='2017-01-01 00:00:00', periods = periods, freq='T') 

df = pd.DataFrame(data, index = idx, columns=['value']) 

fig, ax = plt.subplots() 
fig.autofmt_xdate() 

ax.bar(df.index, df['value']) 

plt.show() 
time.sleep(5) 

返回下列图:

enter image description here

改变频率到d,

idx = pd.date_range(start='2017-01-01 00:00:00', periods = periods, freq='D') 

产生了良好的曲线图。

enter image description here

什么是第一张图的问题?

回答

1

问题是酒吧的宽度(太宽)。 尝试:

ax.bar(df.index, df['value'], width=1/24/60/2) 

您可能还需要调整刻度标签格式,例如:

ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%T')) 
+0

您的解决方案很好地工作。非常感谢你。 如果我想让酒吧彼此相邻,我相信宽度应该是1/24/60,并且对于8H 1/3。 我将使用edgecolor属性来提供视觉边界。 非常感谢您再次。最好的祝福。 – user3225309

+0

是否有动态设置颜色的方法? 例如,如果该值低于5,则color = blue,否则color = red。 – user3225309

+0

是的,颜色属性也需要数组:color_mask = ['red'if v <5 else'blue'for v in df ['value'] .value]'and then'ax.bar(df.index,df ['value'],width = 1/24/60/1,color = color_mask)' – piiipmatz

相关问题