Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project!

## Re: [Matplotlib-users] Histogram and changing x-axis limits

 Re: [Matplotlib-users] Histogram and changing x-axis limits From: John Hunter - 2004-01-22 21:54:30 ```>>>>> "Engelsma," == Engelsma, Dave writes: Engelsma,> Why are the x-axis limits adjusted when I try to plot a Engelsma,> vertical line as a Control Limit? Everytime you add some data to the plot, matplotlib recomputes the axis limits. The quick fix for you is to set your xlim after all the plot commands # the histogram of the data n, bins, patches = matplotlib.matlab.hist(histogram_data, 10, normed=0) # add a 'best fit' line y = matplotlib.matlab.normpdf(bins, average[i], std_dev[i]) l = matplotlib.matlab.plot(bins, y, 'r--') matplotlib.matlab.set(l, 'linewidth', 2) matplotlib.matlab.xlabel(DataDescription[i]) matplotlib.matlab.ylabel('Number of Parts') matplotlib.matlab.plot([201,201],[0,40],'k--') matplotlib.matlab.title(PartNum + " -- " + PartDesc) matplotlib.matlab.set(matplotlib.matlab.gca(), 'xlim', [199, 205]) matplotlib.matlab.show() But it's not clear to me why the autoset would leave some of your data offscreen - that shouldn't happen. I'll take a look at that code. Is it possible for you to provide a complete example that replicates the problem? On an unrelated note, give that you like to use the fully qualified names of all the functions, you might prefer the object oriented interface n, bins, patches = matplotlib.matlab.hist(histogram_data, 10, normed=0) ax = matplotlib.matlab.subplot(111) y = matplotlib.mlab.normpdf(bins, average[i], std_dev[i]) ^ normpdf is defined in mlab.py, not matlab.py lines = ax.plot(bins, y, 'r--') for line in lines: line.set_linewidth(2) ax.set_xlabel(DataDescription[i]) ax.set_ylabel('Number of Parts') ax.plot([201,201],[0,40],'k--') ax.set_title(PartNum + " -- " + PartDesc) ax.set_xlim([199, 205]) matplotlib.matlab.show() Hope this helps, John Hunter ```

### Thread view

 [Matplotlib-users] Histogram and changing x-axis limits From: Engelsma, Dave - 2004-01-22 21:18:21 Attachments: Message as HTML ```Hi - I'm using the following code to generate a histogram. I also want to plot the Upper Control Limit as a vertical line to the right of the histogram bars and a Lower Control Limit to the left of the bars. Each Control Limit would be represented by a vertical line. The code below does not include the line necessary to plot a sample control limit. # the histogram of the data n, bins, patches = matplotlib.matlab.hist(histogram_data, 10, normed=0) # add a 'best fit' line y = matplotlib.matlab.normpdf(bins, average[i], std_dev[i]) l = matplotlib.matlab.plot(bins, y, 'r--') matplotlib.matlab.set(l, 'linewidth', 2) matplotlib.matlab.set(matplotlib.matlab.gca(), 'xlim', [199, 205]) matplotlib.matlab.xlabel(DataDescription[i]) matplotlib.matlab.ylabel('Number of Parts') matplotlib.matlab.title(PartNum + " -- " + PartDesc) matplotlib.matlab.show() When I modify the code with the additional line to plot a Lower Control Limit: # the histogram of the data n, bins, patches = matplotlib.matlab.hist(histogram_data, 10, normed=0) # add a 'best fit' line y = matplotlib.matlab.normpdf(bins, average[i], std_dev[i]) l = matplotlib.matlab.plot(bins, y, 'r--') matplotlib.matlab.set(l, 'linewidth', 2) matplotlib.matlab.set(matplotlib.matlab.gca(), 'xlim', [199, 205]) matplotlib.matlab.xlabel(DataDescription[i]) matplotlib.matlab.ylabel('Number of Parts') matplotlib.matlab.plot([201,201],[0,40],'k--') <--- added this line to plot a control limit matplotlib.matlab.title(PartNum + " -- " + PartDesc) matplotlib.matlab.show() The x-axis limits are somehow automatically forced to something less than that specified in the line: matplotlib.matlab.set(matplotlib.matlab.gca(), 'xlim', [199, 205]). The histogram appears as would be expected, but now I cannot see my Control Limit (specified in the added line of code) as it seems to be plotted off of the chart. If I comment out the additional line (that plotted a vertical line as a control limit), the x-axis limits return to normal, as expected (199 -> 205). Why are the x-axis limits adjusted when I try to plot a vertical line as a Control Limit? Thanks in advance, Dave Engelsma ```
 Re: [Matplotlib-users] Histogram and changing x-axis limits From: John Hunter - 2004-01-22 21:54:30 ```>>>>> "Engelsma," == Engelsma, Dave writes: Engelsma,> Why are the x-axis limits adjusted when I try to plot a Engelsma,> vertical line as a Control Limit? Everytime you add some data to the plot, matplotlib recomputes the axis limits. The quick fix for you is to set your xlim after all the plot commands # the histogram of the data n, bins, patches = matplotlib.matlab.hist(histogram_data, 10, normed=0) # add a 'best fit' line y = matplotlib.matlab.normpdf(bins, average[i], std_dev[i]) l = matplotlib.matlab.plot(bins, y, 'r--') matplotlib.matlab.set(l, 'linewidth', 2) matplotlib.matlab.xlabel(DataDescription[i]) matplotlib.matlab.ylabel('Number of Parts') matplotlib.matlab.plot([201,201],[0,40],'k--') matplotlib.matlab.title(PartNum + " -- " + PartDesc) matplotlib.matlab.set(matplotlib.matlab.gca(), 'xlim', [199, 205]) matplotlib.matlab.show() But it's not clear to me why the autoset would leave some of your data offscreen - that shouldn't happen. I'll take a look at that code. Is it possible for you to provide a complete example that replicates the problem? On an unrelated note, give that you like to use the fully qualified names of all the functions, you might prefer the object oriented interface n, bins, patches = matplotlib.matlab.hist(histogram_data, 10, normed=0) ax = matplotlib.matlab.subplot(111) y = matplotlib.mlab.normpdf(bins, average[i], std_dev[i]) ^ normpdf is defined in mlab.py, not matlab.py lines = ax.plot(bins, y, 'r--') for line in lines: line.set_linewidth(2) ax.set_xlabel(DataDescription[i]) ax.set_ylabel('Number of Parts') ax.plot([201,201],[0,40],'k--') ax.set_title(PartNum + " -- " + PartDesc) ax.set_xlim([199, 205]) matplotlib.matlab.show() Hope this helps, John Hunter ```