1

Hey I have the following problem. I have a array like this:

 arr1=
[[4 4 4]
 [4 4 6]
 [4 3 4]
 [4 4 7]
 [4 4 3]
 [4 4 1]
 [3 4 7]
 [4 3 7]
 [4 4 5]
 [4 3 6]]

Now i want to get a stacked bar plot(histogram), that shows the number of different elements,, like this:

enter image description here

My frist approach was to bincount the elements, fill the arrays up, but then i dont know what to do.

arr2=
    [[0 0 0]
     [0 0 1]
     [0 0 0]
     [1 3 1]
     [9 7 2]
     [0 0 1]
     [0 0 2]
     [0 0 3]]
2
  • 1
    What code have you tried so far? Commented Jul 5, 2017 at 11:37
  • @Varlor: Always useful to give a MWE. See here: stackoverflow.com/help/mcve Commented Jul 5, 2017 at 11:48

2 Answers 2

2

Here is the output

enter image description here

and the code to generate the same

import numpy as np
import pandas as pd
data = np.array([[4, 4, 4],
 [4, 4, 6],
 [4, 3, 4],
 [4, 4, 7],
 [4, 4, 3],
 [4, 4, 1],
 [3, 4, 7],
 [4, 3, 7],
 [4, 4, 5],
 [4, 3, 6]])

columns = ['Col1', 'Col2', 'Col3']
df = pd.DataFrame(data, columns=columns)
out = {}
for column in columns:
    out[column] = pd.value_counts(df[column])

uniq_df = pd.DataFrame(out).fillna(0)

uniq_df.T.plot(kind="bar", stacked=True)
Sign up to request clarification or add additional context in comments.

5 Comments

Also, import pandas as pd :)
@DavidG : Thanks. Corrected.
Is it possible to change the position of the legend?
Ok i will search for it. Do you know how i could save that plot? If i try to save it, it always saves a matplot plot
Is it possible to add a number to legend, althoug it do not appear in data, like the number "2", so that the colormap is the same to data which include th enumber 2 ?
2

Adding an Altair based answer as well.

import numpy as np
import pandas as pd
from altair import *
data = np.array([[4, 4, 4],
 [4, 4, 6],
 [4, 3, 4],
 [4, 4, 7],
 [4, 4, 3],
 [4, 4, 1],
 [3, 4, 7],
 [4, 3, 7],
 [4, 4, 5],
 [4, 3, 6]])

columns = ['Col1', 'Col2', 'Col3']
df = pd.DataFrame(data, columns=columns)
df = df.T.stack().reset_index(level=[0,1])
df.columns = ['Col','RowNum','Value']
Chart(df).mark_bar().encode(y='count(*)', x='Col:N', color='Value:N')

enter image description here

5 Comments

Is t possible to make the bars in this altair based approach wider? :)
It also says that "Chart" in the last line in your code is not defined and not a module of pandas
It's from altair. You'd need to install altair and then do: from altair import *
hmm ok, i installed it via conda, but it still says: 'module' object has no attribute 'Chart'
Did you restart the kernel after installing?

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.