我的原始数据集为no。每班每天生产的物品:
x = [{'day' : 1, 'shift' : 1, 'count':3},
{'day' : 1, 'shift' : 2, 'count':5},
{'day' : 1, 'shift' : 3, 'count':7},
{'day' : 2, 'shift' : 1, 'count':2},
{'day' : 2, 'shift' : 2, 'count':4},
{'day' : 2, 'shift' : 3, 'count':6},]
x_df = pd.DataFrame(x)
x_df = x_df[['day', 'shift', 'count']]
我想绘制每日图表,并列每个班次。我认为首先要做的是将day
设为x轴,将shift_1
设为1列,将shift_2
设为1列,将shift_3
设为另一列。大熊猫有一种简单的方法吗?
答案 0 :(得分:1)
对于散点图,这应该有效
import pylab
import pandas as pd
x = [{'day' : 1, 'shift' : 1, 'count':3},
{'day' : 1, 'shift' : 2, 'count':5},
{'day' : 1, 'shift' : 3, 'count':7},
{'day' : 2, 'shift' : 1, 'count':2},
{'day' : 2, 'shift' : 2, 'count':4},
{'day' : 2, 'shift' : 3, 'count':6},]
x_df = pd.DataFrame(x)
x_df = x_df[['day', 'shift', 'count']]
for s in x_df['shift'].unique():
x = x_df[x_df['shift'] == s]
pylab.plot(x['day'],x['count'],label="shift %i" %s)
pylab.legend(loc='best')
pylab.ylabel("count")
pylab.xlabel("day")
输出
表示聚类条形图:
import matplotlib.pyplot as plt
import pandas as pd
from numpy import *
x = [{'day' : 1, 'shift' : 1, 'count':3},
{'day' : 1, 'shift' : 2, 'count':5},
{'day' : 1, 'shift' : 3, 'count':7},
{'day' : 2, 'shift' : 1, 'count':2},
{'day' : 2, 'shift' : 2, 'count':4},
{'day' : 2, 'shift' : 3, 'count':6},]
x_df = pd.DataFrame(x)
x_df = x_df[['day', 'shift', 'count']]
w = .3 #set width of bar
fig, ax = plt.subplots()
shifts = x_df['shift'].unique()
for s in shifts:
x = x_df[x_df['shift'] == s]
counts = array(x['count'])
inds = arange(len(counts))
plt.bar(inds*w*1.1+s,counts,w)
#pretty up xaxis
ax.set_xticks(shifts+w*max(inds)/2)
ax.set_xticklabels(shifts)
plt.ylabel("count")
plt.xlabel("shift")
调整条形聊天请参见手册: https://matplotlib.org/gallery/units/bar_unit_demo.html