2
import matplotlib.pyplot as plt
import datetime
x = [datetime.datetime(1943,3, 13,12,0,0),
     datetime.datetime(1943,3, 13,12,5,0),
     datetime.datetime(1943,3, 13,12,10,0),
     datetime.datetime(1943,3, 13,12,15,0),
     datetime.datetime(1943,3, 13,12,20,0),
     datetime.datetime(1943,3, 13,12,25,0),
     datetime.datetime(1943,3, 13,12,30,0),
     datetime.datetime(1943,3, 13,12,35,0)]
y = [1,2,3,4,2,1,3,4]

# plot the data out but does not provide sufficient detail on the lower    values
plt.figure()
plt.bar(x,y)

# plot the data out but ommit the datetime information
plt.figure()
plt.bar(range(0,len(x)),y)

Hello guys, I am just starting with the matplotlib in transition from matlab to python. However, I encountered weird behavior of matplotlib as it is not able to display the data along with the datetime element. My question here would be the output of both bar plot yield two different results.

enter image description here

The first one directly convert the data into some kind of continuous data where as the second one more like categorical data. Do anyone encountered similar problem as mine and dont mind share their way of approaching this?

P/s: i tried seaborn and it works but somehow does not play well with dual axis plotting. I also googled for similar issue but somehow not such issue?

4

2 回答 2

4

我不确定我是否会将观察到的行为称为意外。在第一种情况下,您为条形图的 x 变量提供日期,因此它将在这些日期绘制条形图。在第二种情况下,您为 x 变量提供了一些数字,因此它将绘制数字。

由于您没有说出您真正喜欢哪一个,因此解决方案是使它们在视觉上相等。不过,各自的概念是不同的。

import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
import datetime
x = [datetime.datetime(1943,3, 13,12,0,0),
     datetime.datetime(1943,3, 13,12,5,0),
     datetime.datetime(1943,3, 13,12,10,0),
     datetime.datetime(1943,3, 13,12,15,0),
     datetime.datetime(1943,3, 13,12,20,0),
     datetime.datetime(1943,3, 13,12,25,0),
     datetime.datetime(1943,3, 13,12,30,0),
     datetime.datetime(1943,3, 13,12,35,0)]
y = [1,2,3,4,2,1,3,4]

# plot numeric plot
plt.figure()
plt.bar(x,y, width=4./24/60) # 4 minutes wide bars
plt.gca().xaxis.set_major_formatter(DateFormatter("%H:%M"))

# Plot categorical plot
plt.figure()
plt.bar(range(0,len(x)),y, width=0.8) # 0.8 units wide bars
plt.xticks(range(0,len(x)), [d.strftime("%H:%M") for d in x])

plt.show()

在此处输入图像描述

然而,当使用不同的数据时,概念之间的差异会更明显,

x = [datetime.datetime(1943,3, 13,12,0,0),
     datetime.datetime(1943,3, 13,12,5,0),
     datetime.datetime(1943,3, 13,12,15,0),
     datetime.datetime(1943,3, 13,12,25,0),
     datetime.datetime(1943,3, 13,12,30,0),
     datetime.datetime(1943,3, 13,12,35,0),
     datetime.datetime(1943,3, 13,12,45,0),
     datetime.datetime(1943,3, 13,12,50,0)]

在此处输入图像描述

于 2018-11-21T04:21:33.170 回答
0

我不确定如何解决 and 的问题matplotlibdatetime但可以很好地pandas处理对象。datetime你可以考虑一下。例如,您可以执行以下操作:

import pandas as pd
df = pd.DataFrame({'date': x, 'value': y})
df.set_index('date').plot.bar()
plt.show()

熊猫结果

改进也很容易做到:

df = pd.DataFrame({'date': x, 'value': y})
df['date'] = df['date'].dt.time 
df.set_index('date').plot.bar(rot=0, figsize=(10, 5), alpha=0.7)
plt.show()

图 2

于 2018-11-21T03:44:27.017 回答