-1

I need to draw a bar graph for the values:

 male=('2', '1', '2', '6', '6', '1') # list may increase
 time=('Tue_Aug_13_04:37:40_2013', 'Mon_Jul__1_02:33:11_2013','Tue_Aug_13_04:37:40_2013', 'Thu_Jul__4_01:53:32_2013', 'Mon_Jul__1_10:05:55_2013','Mon_Jul__1_04:15:25_2013')# list may increase
female=(16, 11, 16, 12, 12, 11) # list may increase  

Male in green colour, female in red colour as the image attached below:

example-plot

The code which I tried:

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Polygon

fig = plt.figure()
ax1 = fig.add_subplot(131)
ax1.bar(male, color='red', edgecolor='black')
ax1.bar(bottom=range(female), color='blue', edgecolor='black')
ax1.set_xticks(time)
plt.show()

What modifications do I need to make in order to draw the bar graph as shown in the image attached for my values?

Schorsch
  • 7,761
  • 6
  • 39
  • 65
  • What do you want to do with the `time`? What should be your `x`-axis? Does [this question/answer](http://stackoverflow.com/questions/5902371/matplotlib-bar-chart-with-dates) help? – Schorsch Mar 25 '14 at 10:19
  • Hi Schorsch,Time should be in x-axis and y axis as both male and female count with different colour with count dispalyed on it. – user2558589 Mar 25 '14 at 10:23

1 Answers1

3

1.) I strongly suggest that you familiarize yourself with the python syntax:

2.) Make use of the matplotlib documentation to figure out the correct syntaxt for the plot commands you are using.

3.) In this particular case: To get you going, change your data to:

male=[2, 1, 2, 6, 6, 1] # list may increase  
time=['Tue_Aug_13_04:37:40_2013', 'Mon_Jul__1_02:33:11_2013','Tue_Aug_13_04:37:40_2013', 'Thu_Jul__4_01:53:32_2013', 'Mon_Jul__1_10:05:55_2013','Mon_Jul__1_04:15:25_2013']# list may increase  
female=[16, 11, 16, 12, 12, 11] # list may increase

Please examine carefully what has changed.

4.) The bar command you try to call has not enough input arguments. With the changed data from above, try this:

ax1.bar(range(len(time)),male,width=0.5, color='red', edgecolor='black')
ax1.bar(range(len(time)),female,width=0.5,bottom=male,color='blue', edgecolor='black')

What has changed?

  • you need the following inputs: left, height, width=0.8
  • you had only one of those
  • due to the fact that your dates are given as strings, you need a generic counter for the x-axis, hence the range(len(time)) to provide as many tics as there are entries in time.
  • now, you specify the height according to the values in male and female - none of which should be strings!
  • define a width
  • in your case, you want the bars to be stacked - therefore, specify the first set of values as bottom for the second

4.) Because time is made up of strings, you cannot use it for the ticks. Instead, try:

ax1.set_xticklabels(time,rotation=90)

Here, you use the strings from time as tick-labels. The rotation=90 is a nice feature so that the long strings do not overlap.

5.) If the labels are cut off by the plot window, try this:

plt.tight_layout()
plt.show()  

This should get you back on track.


Good key words for a web-search inlcude:

  • matplotlib stacked bar
  • matplotlib tick labels rotation
  • matplotlib ticks date
Community
  • 1
  • 1
Schorsch
  • 7,761
  • 6
  • 39
  • 65