1

I have data of factories and their error codes during production such as below;

PlantID  A  B  C  D
1        0  1  2  4
1        3  0  2  0
3        0  0  0  1
4        0  1  1  5

Each row represent production order. I want to create a graph with x-axis=PlantID's and y-axis are A,B,C,D with different bars. In this way I can see that which factory has the most D error, which has A in one graph

I usually use plotly and seaborn but I couldn't find any solution for that, y-axis is single column in every example

Thanks in advance,

2 Answers 2

3

Seaborn likes its data in long or wide-form.

As mentioned above, seaborn will be most powerful when your datasets have a particular organization. This format ia alternately called “long-form” or “tidy” data and is described in detail by Hadley Wickham in this academic paper. The rules can be simply stated:

  • Each variable is a column
  • Each observation is a row

The following code converts the original dataframe to a long form dataframe. By stacking the columns on top of each other such that every row corresponds to a single record that specifies the column name and the value (the count).

import numpy as np
import pandas as pd
import seaborn as sns
# Generating some data
N = 20
PlantID = np.random.choice(np.arange(1, 4), size=N, replace=True)
data = dict((k, np.random.randint(0, 50, size=N)) for k in ['A', 'B', 'C', 'D'])
df = pd.DataFrame(data, index=PlantID)
df.index = df.index.set_names('PlantID')
# Stacking the columns and resetting the index to create a longformat. (And some renaming)
df = df.stack().reset_index().rename({'level_1' : 'column', 0: 'count'},axis=1)
sns.barplot(x='PlantID', y='count', hue='column', data=df)

Barplot result

Sign up to request clarification or add additional context in comments.

Comments

0

Pandas has really clever built-in plotting functionality:

df.plot(kind='bar')
plt.show()

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.