4

I would like to plot the corresponding x for a given name. by that I mean, for foo it has to plot [10,20,30] in the form of a histogram and all foo, bar, baz need to be in the same graph.(I don't require 3d :) )

import pylab as P
name = ['foo', 'bar', 'baz']
x  = [[10,20,30],[40,50,60],[70,80,90]]

P.figure()
P.hist(x, 10,  histtype='bar',
                color=['crimson', 'burlywood', 'chartreuse'],
                label=['Crimson', 'Burlywood', 'Chartreuse'])

P.show()
2
  • please explain better what you need. what does it mean to plot [10,20,30] as an histogram ? Commented Nov 13, 2013 at 20:39
  • Can you help me plot the graph the in the figure attached?. I tried but I failed. Commented Nov 13, 2013 at 20:52

1 Answer 1

6

Hope this helps you:

from matplotlib import pyplot as plt
import numpy as np

names = ['foo', 'bar', 'baz']
x  = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
colors = ['crimson', 'burlywood', 'chartreuse']

y = zip(*x)
groups = len(x)
members = len(y)
pos = np.arange(groups)
width = 1. / (1 + members)

fig, ax = plt.subplots()    
for idx, (serie, color) in enumerate(zip(y, colors)):
    ax.bar(pos + idx * width, serie, width, color=color)

ax.set_xticks(pos + width)
ax.set_xticklabels(names)

plt.show()

enter image description here

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

2 Comments

I suggest ax.set_xticks(pos + 1.5*width) to center the labels under the groups
@spinup right thanks, We can let the OP to optimize the view and understand what is doing what...

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.