Re: [Matplotlib-users] histogram and a line

 Re: [Matplotlib-users] histogram and a line From: Jerzy Karczmarczuk - 2012-03-07 08:44:39 Attachments: Message as HTML ```Mic: > Hello, > I am able to draw a histogram with the following code: > > /.../ > /h.hist(hist_data, bins=50, normed=True)/ > > However, I don't know how to draw a line for median at 249 position > like in attachment. Are your axes really matplotlib axes? (I have doubts, since the vertical is not normed, although your histogram is). In your real axes, the command plot([249,249],[0,height]) draws a vertical line (there is a primitive vert. line as well), what is the problem?? Jerzy Karczmarczuk ```

 Re: [Matplotlib-users] histogram and a line From: Jerzy Karczmarczuk - 2012-03-07 08:44:39 Attachments: Message as HTML ```Mic: > Hello, > I am able to draw a histogram with the following code: > > /.../ > /h.hist(hist_data, bins=50, normed=True)/ > > However, I don't know how to draw a line for median at 249 position > like in attachment. Are your axes really matplotlib axes? (I have doubts, since the vertical is not normed, although your histogram is). In your real axes, the command plot([249,249],[0,height]) draws a vertical line (there is a primitive vert. line as well), what is the problem?? Jerzy Karczmarczuk ```
 Re: [Matplotlib-users] histogram and a line From: Nicolas Rougier - 2012-03-08 08:26:36 ``` You need to specify the ylim because your height may be larger than your histogram and then you cannot see it. Here is a script that reproduce your screenshot (with random data). import numpy as np import matplotlib.pyplot as plt plt.rc('xtick', direction = 'out') plt.rc('ytick', direction = 'out') data = np.random.normal(0,1,100) fig = plt.figure(figsize=(8,6), dpi=72, facecolor='w') axes = plt.subplot(111) axes.axvline(np.mean(data), 0, data.max(), linewidth=2, color='red') axes.hist(data, bins=50, normed=True) axes.set_ylim(0, 1) axes.set_xlim(-3, 3) axes.spines['right'].set_color('none') axes.spines['top'].set_color('none') axes.xaxis.set_ticks_position('bottom') axes.spines['bottom'].set_position(('data', -0.05)) axes.yaxis.set_ticks_position('left') axes.spines['left'].set_position(('data',-3.25)) plt.show() Nicolas On Mar 8, 2012, at 7:54 , Mic wrote: > Hi Jerzy, > Thank you for your answer. With the code below I get only a green line (see attached line.png): > > import numpy as np > import matplotlib.pyplot as plt > > hist_data = [] > > heigth = max(hist_data) + 10 > mean = np.mean(hist_data) > > f = plt.figure() > h = f.add_subplot(111) > h.hist(hist_data, bins=50, normed=True) > h.plot([mean,mean],[0,height]) > f.savefig('myhist.png') > > without h.plot() I get a histogram (see attached matplot_hist.png). > > How can I get the line and the histogram at the same time in the picture? > How is it possible to norm the vertical axis in order to get similar results like in this picture (see attached hist.png)? > > Thank you in advance. > > On Wed, Mar 7, 2012 at 6:41 PM, Jerzy Karczmarczuk wrote: > Mic: >> Hello, >> I am able to draw a histogram with the following code: >> >> ... >> h.hist(hist_data, bins=50, normed=True) >> >> However, I don't know how to draw a line for median at 249 position like in attachment. > Are your axes really matplotlib axes? (I have doubts, since the vertical is not normed, although your histogram is). > > In your real axes, the command plot([249,249],[0,height]) draws a vertical line (there is a primitive vert. line as well), what is the problem?? > > Jerzy Karczmarczuk > > > > ------------------------------------------------------------------------------ > Virtualization & Cloud Management Using Capacity Planning > Cloud computing makes use of virtualization - but cloud computing > also focuses on allowing computing to be delivered as a service. > http://www.accelacomm.com/jaw/sfnl/114/51521223/ > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > > ------------------------------------------------------------------------------ > Virtualization & Cloud Management Using Capacity Planning > Cloud computing makes use of virtualization - but cloud computing > also focuses on allowing computing to be delivered as a service. > http://www.accelacomm.com/jaw/sfnl/114/51521223/_______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users ```
 Re: [Matplotlib-users] histogram and a line From: Jerzy Karczmarczuk - 2012-03-08 12:50:57 ```Nicolas Rougier, (to Mic, who can't see a histogram and a line simultaneously): > You need to specify the ylim because your height may be larger than your histogram and then you cannot see it. I suspect something similar. The height of matplot_hist.png is 0.040. The line goes from 0 to 300. The histogram reduces to the bottom line. There is no immediate relation between max(hist_data) and the height of a *normed* histogram. And, please, if you want to point out the difficulties in making your programs fulfil your aims, show the REAL, COMPLETE programs, perhaps with fake, random data, but without useless : "hist_data=[]" And don't call a mean: "median" or vice-versa... Scaling the axes is easy. "ylim" should be changed, use also the "extent" parameter (see doc). Drawing axes shifted, as in hist.png is a bit more involved, and I don't know if you really want that as well. Jerzy K. ```
 Re: [Matplotlib-users] histogram and a line From: Paul Hobson - 2012-03-08 23:51:58 ```Jerzy et al, Check out the axvline method (of pyplot or an axes object). You'll only have to specify the x-value, and it'll won't rescale your y-axis. -paul On Thu, Mar 8, 2012 at 4:50 AM, Jerzy Karczmarczuk wrote: > Nicolas Rougier, (to Mic, who can't see a histogram and a line > simultaneously): > >> You need to specify the ylim because your height may be larger than your histogram and then you cannot see it. > I suspect something similar. The height of matplot_hist.png is 0.040. > The line goes from 0 to 300. The histogram reduces to the bottom line. > There is no immediate relation between max(hist_data) and the height of > a *normed* histogram. > > And, please, if you want to point out the difficulties in making your > programs fulfil your aims, show the REAL, COMPLETE programs, perhaps > with fake, random data, but without useless : "hist_data=[]" > > And don't call a mean: "median" or vice-versa... > > Scaling the axes is easy. "ylim" should be changed, use also the > "extent" parameter (see doc). Drawing axes shifted, as in hist.png is a > bit more involved, and I don't know if you really want that as well. > > Jerzy K. > > > ------------------------------------------------------------------------------ > Virtualization & Cloud Management Using Capacity Planning > Cloud computing makes use of virtualization - but cloud computing > also focuses on allowing computing to be delivered as a service. > http://www.accelacomm.com/jaw/sfnl/114/51521223/ > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users ```
 Re: [Matplotlib-users] histogram and a line From: Jerzy Karczmarczuk - 2012-03-12 10:48:21 Attachments: Message as HTML ```Mic : > Thank you for your solutions. However, with my dataset it did not work > out with the following code: > > > ... > > Please find attached the histogram. The dataset I am sending with > next email. > 1. You haven'sent *in.small.txt*, but *in.txt*, 70 MB, several millions lines. Please, be gentle wrt people who want to help you and are obliged to test your stuff. 2. You file is buggy, last two lines generate an exception. They contain a text "mean ...". 3. I corrected them, the program works, and gives the picture you attached. You don't say WHAT is "not working". Is it the question of scaling, or something else? 4 If it is scaling, and IF IF ... you want to obtain something I attach, then use the code: *hst=axes.hist(data, bins=50, normed=True) mxh=max(hst[0]) plt.plot([mean,mean],[0,mxh],'r',lw=2) * 5. Learn something about WHAT is a normed histogram in matplotlib, how to get into the plotted data, and to adjust the scales. Otherwise such problems will repeat. 6. Finally, in our civilized world people sign their messages. Anonymous letters are ugly. Jerzy Karczmarczuk ```