473,499 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Matplotlib: Histogram with bars inside grid lines...how??

I'm playing around with matplotlib for the first time. I'm trying to
make a very simple histogram of values 1-6 and how many times they
occur in a sequence. However, after about an hour of searching I cannot
make the histogram stay within the bounds of the grid lines.

Here is my example:

pylab.grid()
x_values=[1,1,2,2,2,3,3,3,4,4,4,5,5,6,6,6]
pylab.hist(x_values,6)
pylab.show()

This produced the following image:
http://enigmacurry.com/usenet/histor...grid-lines.png

Starting with bar number 2, it creeps into grid 1.. and finally with
bar number 5 it's almost entirely in grid 4.. how do I make the bars
stay in their own grid lines?

I can see that hist() is somehow derived from bar() ... so it appears
that hist() has some undocumented parameters. I tried specifiying
width=1 but that just squished all of the bars together.

Also, is there a more object-oriented graphing package for Python? (How
am I supposed to know that hist() is derived from bar() if the docs
don't show proper inheritance?)

Thanks!

Mar 30 '06 #1
5 11756
Two things:

1) I now see where width is defined in the hist() documentation... I
was expecting it to be in the definition up at the top, but instead the
definition has **kwords.. not very helpful.

2) I noticed in my original historgram, that the y scale was not the
same as the x scale.. so I updated my code:

import pylab

pylab.grid()
x_values=[1,1,2,2,2,3,3,3,4,4,4,5,5,6,6,6]
pylab.hist(x_values,6)
pylab.yticks(pylab.arange(3))
pylab.axis('scaled')
pylab.show()
It produced this image:
http://enigmacurry.com/usenet/histor...rid-lines2.png

It has the same problem..

Mar 30 '06 #2
>>>>> "Enigma" == Enigma Curry <wo*****@gmail.com> writes:

Enigma> I'm playing around with matplotlib for the first time. I'm
Enigma> trying to make a very simple histogram of values 1-6 and
Enigma> how many times they occur in a sequence. However, after
Enigma> about an hour of searching I cannot make the histogram
Enigma> stay within the bounds of the grid lines.

Enigma> Here is my example:

Enigma> pylab.grid() x_values=[1,1,2,2,2,3,3,3,4,4,4,5,5,6,6,6]
Enigma> pylab.hist(x_values,6) pylab.show()

Enigma> This produced the following image:
Enigma> http://enigmacurry.com/usenet/histor...grid-lines.png

Enigma> Starting with bar number 2, it creeps into grid 1.. and
Enigma> finally with bar number 5 it's almost entirely in grid
Enigma> 4.. how do I make the bars stay in their own grid lines?

While exactly what you want is something of an enigma to me, I can
offer some advice and terminology. The bars of hist make no attempt
to stay within the bounds of the grid lines... The bars have as their
left most boundary the bins you choose for the histogram. As a first
step, I suggest setting these bins explicitly, rather than letting the
hist command choose them automatically

from pylab import hist, xlim, nx, show

x_values= [1,1,2,2,2,3,3,3,4,4,4,5,5,6,6,6]
bins = nx.arange(0.5, 7.)
hist(x_values, bins)
xlim(0,6.5)
show()

The grid line locations are determined by the xtick locations, which
you can set with the xticks command.

Good luck!
JDH
Mar 30 '06 #3
Thank you John. Your explanation helped a lot!

In case it helps anyone else in the future, here is my code for
*exactly* what I was after:

import pylab

def ryan_hist(data, bar_width, min_x, max_x):
"""
Create a frequency histogram over a continuous interval

min_x = the low end of the interval
max_x = the high end of the interval

bar_width = the width of the bars

This will correctly align the bars of the histogram
to the grid lines of the plot
"""
#Make histogram with bars of width .9 and center
#them on the integer values of the x-axis
bins = pylab.nx.arange(1-(bar_width/2),max(data))
n,bins,patches = pylab.hist(data, bins, width=bar_width)

#Make Y axis integers up to highest n
pylab.yticks(pylab.arange(sorted(n)[-1]))
pylab.axis('scaled')
pylab.xlim(0.5,6.5)
pylab.grid()
pylab.show()

#Create a historgram
data=[1,1,2,2,2,2,2,2,3,3,3,4,4,4,5,5,6,6,6]
bar_width = 0.9
ryan_hist(data,bar_width,min(data),max(data))

Mar 30 '06 #4
pylab.xlim(0.5,6.5)

should be:

pylab.xlim(min_x-(bar_width/2),max_x+(bar_width/2))

Mar 30 '06 #5
>>>>> "Enigma" == Enigma Curry <wo*****@gmail.com> writes:

Enigma> pylab.xlim(0.5,6.5) should be:

Enigma> pylab.xlim(min_x-(bar_width/2),max_x+(bar_width/2))

Glad it's working better for you -- just a couple more smallish hints.

You might prefer to have your grid lines behind, rather than above the
bars. In that case create the subplot or axes with the axisbelow=True
kwarg. Despite the fact that you found the kwargs a little annoying
at first, you will probably come to love them. matplotlib makes very
heavy use of them and they are very useful since they allow matplotlib
to usually do the right things while exposing most of the settings to
you. Eg

plot(x, y,
linewidth=2, linestyle='--',
marker='o', markerfacecolor='r', markeredgecolor='g'
markeredgewith=2, markersize=10)

and so on. There are lots of properties you can set on almost every
command. Because noone wants to type all that, you can use aliases

plot(x, y, lw=2, ls='--', marker='o', mfc='r', mec='g', mew=2, ms=10)
Secondly, in your example, you are relying implicitly on matplotlib to
pick integer ticks for the xaxis. It's doing it right in this
example, but might prefer other tick locations for other examples
depending on your x_values. So set xticks explicitly.

Below is a slightly modified example showing these two ideas.

You also might want to consider joining the mailing list at

http://lists.sourceforge.net/mailman...tplotlib-users

since you appear to be a little finicky about your figures :-)

def ryan_hist(data, bar_width, min_x, max_x):
"""
Create a frequency histogram over a continuous interval

min_x = the low end of the interval
max_x = the high end of the interval

bar_width = the width of the bars

This will correctly align the bars of the histogram
to the grid lines of the plot
"""
#Make histogram with bars of width .9 and center
#them on the integer values of the x-axis
bins = pylab.nx.arange(1-(bar_width/2),max(data))
pylab.subplot(111, axisbelow=True)
n,bins,patches = pylab.hist(data, bins, width=bar_width)

#Make Y axis integers up to highest n
pylab.yticks(pylab.arange(max(n)))
pylab.xticks(pylab.arange(max(n)+1))
pylab.axis('scaled')
pylab.xlim(min_x-(bar_width/2),max_x+(bar_width/2))
pylab.grid()
pylab.show()
Mar 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4974
by: Pekko Piirola | last post by:
I'm trying to make an animation with matplotlib. The problem: whenever I try to rescale or move the plot with the buttons of plotting window, the animation stops. My system is Debian Woody and...
1
5213
by: Jorl Shefner | last post by:
I've only been able to plot data with both symbols and lines by issuing two plot commands, one for markers and one for lines. That's perfectly fine, but it creates a problem when I try to create a...
2
11633
by: Grant Edwards | last post by:
I downloaded examples/contour_demo.py, and it doesn't run. I've searched both the user guide and the Wiki for "contour" and got zero hits. ...
8
5827
by: Andi Clemens | last post by:
Hi, everytime I try to plot a bar with matplotlib I get the following error message: Traceback (most recent call last): File "bar_stacked.py", line 13, in ? p1 = bar(ind, menMeans, width,...
11
3981
by: c19h28o2 | last post by:
Hi, Guy's I know there are several posts about this, however I do not want to read them as answers are undoubtedly posted! Here is my attempt but I'm slightly stuck. I'm not looking for the...
5
11889
by: John Henry | last post by:
I've been asking this question at the matplotlib user list and never gotten an answer. I am hoping that there are matplotlib users here that can help. My problem with matplotlib's way of...
5
2898
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
12
3409
by: orangeDinosaur | last post by:
Hi, I am exploring the possibility of using python as a replacement of MATLAB when I leave school. So, I've been playing with matplotlib and have run into some weird behavior after recently...
0
1521
by: Dick Crepeau | last post by:
I would like to control the y axis of a plot. The following code does exactly what I want it to! On my linux computer it sets the y axis limits to 18.0 minimum, 58.0 maximum, plots some points on...
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4917
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.