0

I have this list of "coordinates":

   myList = [[0.7366771159874608, 0.6270718232044199], [0.7382352941176471, 0.6710182767624021], [0.7967479674796748, 0.656441717791411], [0.7296511627906976, 0.5727109515260324], [0.7992700729927007, 0.5833333333333334], [0.750788643533123, 0.5288888888888889], [0.851063829787234, 0.7423312883435583], [0.767515923566879, 0.5525114155251142]]

I want to create a grouped bar plot so that each of this pairs is close. The names of the column are just numbered from I to 8. I looked on the internet but it doesn't seem to me other people had this problem.

My code:

import matplotlib.pyplot as plt
x, y = zip(*mylist)
group_labels = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII']
plt.bar(x, y)

plt.title("Trial")
plt.show()

How should I change my dataset in order to achieve my goal?

5
  • Please don't name your list of coordinates "list". This term is a built-in type in Python - you will overwrite it in your code, rendering list() unusable. However, I do not understand your desired output. What is a stacked bar chart of x,y coordinates? What is stacked here in each group? Can you provide a sample output from the internet? Commented Oct 18, 2020 at 9:06
  • It is not stacked, but grouped, see this image: link. The pairs of my list need to be grouped like in the image. Is it clearer now? (I also changed the name of the list, thanks!) Commented Oct 18, 2020 at 9:08
  • But this is directly from the matplotlib gallery. What is the difficulty you ran into adapting this example to your needs? Commented Oct 18, 2020 at 9:14
  • I don't know what to put as an input, the examples online don't fit my case. Commented Oct 18, 2020 at 9:15
  • Why not? It seems you can simply substitute the variables: x is men_means, y is women_means, and group_labels is labels. Of course, you should rename your x - the sample code also uses a variable x for a different purpose. Commented Oct 18, 2020 at 9:19

2 Answers 2

1

Adapted from the docs.

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

ar = [[0.7366771159874608, 0.6270718232044199], 
      [0.7382352941176471, 0.6710182767624021], 
      [0.7967479674796748, 0.656441717791411], 
      [0.7296511627906976, 0.5727109515260324], 
      [0.7992700729927007, 0.5833333333333334], 
      [0.750788643533123, 0.5288888888888889], 
      [0.851063829787234, 0.7423312883435583], 
      [0.767515923566879, 0.5525114155251142]
     ]

xx, yy = zip(*ar)
group_labels = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII']
x = np.arange(len(group_labels))
width = 0.35

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, xx, width)
rects2 = ax.bar(x + width/2, yy, width)
ax.set_xticklabels(group_labels)
ax

enter image description here

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

Comments

0

As far as I can tell, you are trying to barplot more than one variables in the same barplot. The grouping that you are mentioning can be actually handled as plotting 2 different variables, man and woman.

Unfortunately, this is not natively implemented in matplotlib python, but you can use pandas to achieve the result you want. THe code is

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
myList = [[0.7366771159874608, 0.6270718232044199], [0.7382352941176471, 0.6710182767624021], [0.7967479674796748, 0.656441717791411], [0.7296511627906976, 0.5727109515260324], [0.7992700729927007, 0.5833333333333334], [0.750788643533123, 0.5288888888888889], [0.851063829787234, 0.7423312883435583], [0.767515923566879, 0.5525114155251142]]
x, y = zip(*myList)
group_labels = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII']
df = pd.DataFrame(np.c_[x, y], index=group_labels)
df.plot.bar()

1 Comment

If you want the matplotlib alternative then @venky__ posted a solution, however it will not work for more variables, in case for example you need 3 or 4, then a parametric function is needed

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.