我有一个DataFrame:
date phone sensor pallet
126 2019-04-15 940203 C0382C391A4D 47
127 2019-04-15 940203 C0382D392A4D 47
133 2019-04-16 940203 C0382C391A4D 47
134 2019-04-16 940203 C0382D392A4D 47
138 2019-04-17 940203 C0382C391A4D 47
139 2019-04-17 940203 C0382D392A4D 47
144 2019-04-18 940203 C0382C391A4D 47
145 2019-04-18 940203 C0382D392A4D 47
156 2019-04-19 940203 C0382D392A4D 47
157 2019-04-19 940203 C0382C391A4D 47
277 2019-04-15 941557 C0392D362735 32
279 2019-04-15 941557 C03633364D50 32
286 2019-04-16 941557 C03633364D50 32
287 2019-04-16 941557 C0392D362735 32
296 2019-04-17 941557 C03633364D50 32
297 2019-04-17 941557 C0392D362735 32
305 2019-04-18 941557 C0392D362735 32
306 2019-04-18 941557 C03633364D50 32
317 2019-04-19 941557 C03633364D50 32
318 2019-04-19 941557 C0392D362735 32
561 2019-04-15 942316 C0384639224D 45
562 2019-04-15 942316 C03632364950 45
563 2019-04-15 942316 C03920363835 45
564 2019-04-15 942316 C0382939384D 45
573 2019-04-16 942316 C0382939384D 45
574 2019-04-16 942316 C0384639224D 45
575 2019-04-16 942316 C03632364950 45
我希望能够为包含每个日期到达的传感器的每个货盘制作子图。
例如:
遍历打开的斧头并绘制每个1
grouped = pallets_arrived.groupby('pallet')
nrows = 2
ncols = 2
fig, axs = plt.subplots(nrows, ncols)
targets = zip(grouped.groups.keys(), axs.flatten())
for i, (key, ax) in enumerate(targets):
ax.plot_date(grouped.get_group(key)['date'], grouped.get_group(key)['sensor'], 'o')
plt.show()
return pallets_arrived
给出格式错误的重复日期(用日期的Df索引不解决问题)
grouped = pallets_arrived.groupby('pallet')
nrows = 2
ncols = 2
fig, axs = plt.subplots(nrows, ncols)
targets = zip(grouped.groups.keys(), axs.flatten())
for i, (key, ax) in enumerate(targets):
grouped.get_group(key).plot(x='date', y='sensor', ax=ax)
ax.legend()
plt.show()
或
grouped = pallets_arrived.set_index('date').groupby('pallet')
nrows = 2
ncols = 2
fig, axs = plt.subplots(nrows, ncols)
targets = zip(grouped.groups.keys(), axs.flatten())
for i, (key, ax) in enumerate(targets):
grouped.get_group(key).plot(grouped.get_group(key).index, y='sensor', ax=ax)
ax.legend()
plt.show()
grouped = pallets_arrived.groupby('pallet')
nrows = 2
ncols = 2
fig, axs = plt.subplots(nrows, ncols)
targets = zip(grouped.groups.keys(), axs.flatten())
for i, (key, ax) in enumerate(targets):
plt.sca(ax)
plt.plot(grouped.get_group(key)['date'], grouped.get_group(key)['sensor'])
ax.legend()
plt.show()
再次
不起作用,因为在同一日期每个货盘中有多个传感器。因此存在重复值错误...
我认为我没有正确获得matplotlib的熊猫包装。 我为自己的学习感到高兴,因为即时阅读指导并无法理解这些东西的首选方法。
非常感谢助手。
答案 0 :(得分:1)
您可以使用matplotlib
来绘制分类数据:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
%matplotlib inline
fig, ax = plt.subplots()
ax.scatter(df['date'], df['sensor'])
plt.show()
或者如果您要为组着色:
fig, ax = plt.subplots()
for _,g in df.groupby('pallet'):
ax.scatter(g['date'], g['sensor'])
plt.show()
您还可以添加图例:
fig, ax = plt.subplots()
for _,g in df.groupby('pallet'):
ax.scatter(g['date'], g['sensor'], label='Pallet_'+str(_))
ax.legend()
plt.show()