【问题标题】:how to plot categorical data?如何绘制分类数据?
【发布时间】:2019-03-28 21:45:26
【问题描述】:

基本上数据包含有关operators 用于特定activitymachine 的信息

df.head()

我有如下数据:

machine_name                activity                      Operator_name     start_datetime       end_datetime   reasons_for_break   duration
Yash [HMC]           PILLAR SUB ASSY MOUNTING ON BASE      Abhishek     2018-10-10 00:50:20 2018-10-10 11:51:23                       661
IMPERIAL             SPINDLE MOTOR ASSEMBLY AND MOUNTING    Abijith     2018-10-10 11:44:00 2018-10-10 12:26:42                        42
V.R                  SPINDLE MOTOR ASSEMBLY AND MOUNTING    Abijith     2018-10-10 11:21:02 2018-10-10 12:26:27                        65
Gnutti Carlo-2[HMC]   ATC MOUNTING ON MACHINE BASE        Anantha Ramu  2018-10-10 00:54:59 2018-10-10 00:55:45                         0
Gnutti Carlo-2[HMC]      SPINDLE MOUNTING                 Anantha Ramu  2018-10-10 00:57:04 2018-10-10 00:58:55    MFD mistake          1
MMF-3[HMC]          APC SUB ASSY MOUNTING ON BASE            Ashok      2018-10-10 09:27:41 2018-10-10 12:04:31    APC UP DOWN         56
MMF-3[HMC]              IT/DDRT MOUNTING ON BASE             Ashok      2018-10-10 13:45:16 2018-10-10 15:13:30                        88
Gnutti Carlo-2[HMC] PILLAR SUB ASSY MOUNTING ON BASE       Balamurali   2018-10-10 09:17:04 2018-10-10 12:21:25                        184
Gnutti Carlo-2[HMC] PILLAR SUB ASSY MOUNTING ON BASE       Balamurali   2018-10-10 12:21:25 2018-10-10 13:18:54    Tea break            57

问题是所有列都是categorical 数据类型,期望start_datetimeend_datetimedatetime 格式,而durationinteger 数据类型。

这些数据怎么能plotted显示它所拥有的所有信息??

我尝试使用 seaborn 作为:

import seaborn as sns
sns.lmplot( x="Operator_name", y="duration", data=df, fit_reg=False, hue='machine_name', legend=True)

但出现错误:

Categorical is not ordered for operation min

如何绘制和显示这些数据的信息??

尝试以下代码:

sns.catplot(x = 'Operator_name' , y = 'duration', hue ='nick_name' , data = df)

得到一个 x 轴重叠的图

【问题讨论】:

  • 你想精确地绘制什么?您希望通过可视化显示哪些信息?

标签: python-2.7 pandas plot seaborn


【解决方案1】:

这是一个虚拟数据帧

df = pd.DataFrame({'Operator_name':["Abhishek"]*4 + ['Abijith']*5 + ['Anamtha Ramu']*3,
                  'Duration': np.random.randint(10, 200, size = 12)})
df.head()

    Operator_name   Duration
0   Abhishek         153
1   Abhishek         188
2   Abhishek         51
3   Abhishek         189
4   Abijith          188

你可以使用 groupby:

df1 = df.groupby('Operator_name').sum().reset_index()
df1

    Operator_name        Duration
0   Abhishek              299
1   Abijith               458
2   Anamtha Ramu          343

使用DataFrame.plot.bar 函数

df1.plot.bar(x = 'Operator_name', y='Duration')

你也可以使用matplotlib:

import matplotlib.pyplot as plt
plt.bar(df['Operator_name'], df['Duration'])

对于 Seaborn,试试下面的代码:

sns.barplot(x = df['Operator_name'], y = df['Duration'], hue = df['machine_name'])

【讨论】:

  • 我还会将machine_name 包括为hue 用于groupby 情节...有可能吗??
  • 任何使用 seaborn 的替代方法??
  • 我已经编辑了我的答案。请让我知道它是否适合您。
  • 使用 seaborn 方式...x 轴上的Operator_name 重叠...可以纠正吗??
  • 更新了主要问题中的图像。请看一下
猜你喜欢
  • 2017-11-20
  • 2019-03-19
  • 1970-01-01
  • 2016-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多