Don Jennings | 26 Jun 2012 13:19
Picon

Re: Looping over histogram plots

> Message: 1
> Date: Tue, 26 Jun 2012 18:40:50 +1000
> From: Elaina Ann Hyde <elainahyde <at> gmail.com>
> To: tutor <at> python.org
> Subject: [Tutor] Looping over histogram plots

<snip>

>        set=(dat['a'+str(index)] == 1.00)

You should not override the builtin set() type [1] as you've done here by assigning it.

> #write the data
>        P.hist(VGSR[set],bins=30, normed=True)

I am not familiar with matplotlib, etc. but given that the primary difference in your two code samples is
where you write the data, I suspect you want something like:

        ax.plot(P.hist(VGSR[set],bins=30, normed=True))

Take care,
Don

[1] http://docs.python.org/library/stdtypes.html#set
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

(Continue reading)

Elaina Ann Hyde | 27 Jun 2012 01:32
Picon

Re: Looping over histogram plots

Dear Don,

    Thanks for the comment, the set type is no problem for me, this is just a variable that I call set... and it works great for my purposes, I do suspect it is something in the way that matplotlib/pyplot deals with histograms, but I have not so far been able to find the right syntax.  Note, the first code example works great, it is only the second (with the hist attempt) that does not do well.  It creates 29 blank plots and 1 histogram instead of 30 histograms... so probably it does need a different phrasing, but the one you suggest gives invalid syntax.  
~Elaina

On Tue, Jun 26, 2012 at 9:19 PM, Don Jennings <dfjennings <at> gmail.com> wrote:
> Message: 1
> Date: Tue, 26 Jun 2012 18:40:50 +1000
> From: Elaina Ann Hyde <elainahyde <at> gmail.com>
> To: tutor <at> python.org
> Subject: [Tutor] Looping over histogram plots

<snip>

>        set=(dat['a'+str(index)] == 1.00)

You should not override the builtin set() type [1] as you've done here by assigning it.

> #write the data
>        P.hist(VGSR[set],bins=30, normed=True)

I am not familiar with matplotlib, etc. but given that the primary difference in your two code samples is where you write the data, I suspect you want something like:

       ax.plot(P.hist(VGSR[set],bins=30, normed=True))

Take care,
Don

[1] http://docs.python.org/library/stdtypes.html#set



--
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Elaina Ann Hyde | 27 Jun 2012 03:44
Picon

Re: Looping over histogram plots

Yay Python:

The solution was a syntax one, if anyone else ever feels like massively multi-plotting histograms, here is the working code:

#----------------------------------------------
fig, axes = plt.subplots(nrows=5, ncols=6, figsize=(12,6))

index=0
for b in axes:
    for ax in b:
        index=index+1
        set=(dat['a'+str(index)] == 1.00)
#write the data
        n, bins, patches = ax.hist(VGSR[set], 30, normed=1)

#label the axis
        if index==13.0:
            ax.set_ylabel('counts')
        if index >= 25.0:
            ax.set_xlabel('VGSR')
plt.show()
#----------------------------------------------- 

~Elaina Hyde
--
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Alan Gauld | 27 Jun 2012 09:47

Re: Looping over histogram plots

On 27/06/12 00:32, Elaina Ann Hyde wrote:

>      Thanks for the comment, the set type is no problem for me, this is
> just a variable that I call set... and it works great for my purposes,

It may work just now but if you ever decide you need to use a Python set 
you will be unable to because you have effectively hidden that data 
type. And it's unlikely to beimmediately obvious why its not working. 
That's why its a bad idea to use the built-in type names as variables.

eg:

myList = [1,2,3,3,4,5,6,3,1,5]
myUniqueList = list(set(mylist))   # remove duplicates

In your program the second line would fail.
If you are confident you will never use sets then its not an issue, but 
these decisions have a habit of coming back to bite you in the future!

--

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Steven D'Aprano | 27 Jun 2012 10:12

Re: Looping over histogram plots

On Wed, Jun 27, 2012 at 08:47:08AM +0100, Alan Gauld wrote:
> On 27/06/12 00:32, Elaina Ann Hyde wrote:
> 
> >     Thanks for the comment, the set type is no problem for me, this is
> >just a variable that I call set... and it works great for my purposes,
> 
> It may work just now but if you ever decide you need to use a Python set 
> you will be unable to because you have effectively hidden that data 
> type. And it's unlikely to beimmediately obvious why its not working. 
> That's why its a bad idea to use the built-in type names as variables.

This is called "shadowing a built-in", and is discouraged for exactly 
the reasons that Alan mentions.

It is much less risky to shadow built-ins inside small functions, so you 
are not surprised that the built-in doesn't work as expected.

When you do it deliberately to change the behaviour of a built-in, it is 
often called "monkey-patching"[1] which is a risky, sometimes useful but 
advanced technique.

Nevertheless, any of these cases should be used with care, and only if 
necessary.

[1] Sometimes called "raving insanity" 
*grin*

--

-- 
Steven
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Mark Lawrence | 27 Jun 2012 10:42
Picon
Favicon

Re: Looping over histogram plots

On 27/06/2012 08:47, Alan Gauld wrote:
> On 27/06/12 00:32, Elaina Ann Hyde wrote:
>
>> Thanks for the comment, the set type is no problem for me, this is
>> just a variable that I call set... and it works great for my purposes,
>
> It may work just now but if you ever decide you need to use a Python set
> you will be unable to because you have effectively hidden that data
> type. And it's unlikely to beimmediately obvious why its not working.
> That's why its a bad idea to use the built-in type names as variables.
>
> eg:
>
> myList = [1,2,3,3,4,5,6,3,1,5]
> myUniqueList = list(set(mylist)) # remove duplicates
>
> In your program the second line would fail.
> If you are confident you will never use sets then its not an issue, but
> these decisions have a habit of coming back to bite you in the future!
>

And they always seem to bite the most tender part of the anatomy that 
they can find :)

--

-- 
Cheers.

Mark Lawrence.

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Gmane