Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib hist() autocropping range

I am trying to make a histgram over a specific range but the matplotlib.pyplot.hist() function keeps cropping the range to the bins with entries in them. A toy example:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform(-100,100,1000)

nbins = 100
xmin = -500
xmax = 500

fig = plt.figure(); 
ax = fig.add_subplot(1, 1, 1)
ax.hist(x, bins=nbins,range=[xmin,xmax])  
plt.show()

Gives a plot with a range [-100,100]. Why is the range not [-500,500] as specified?

(I am using the Enthought Canopy 1.4 and sorry but I do not have a high enough rep to post an image of the plot.)

like image 275
Keith Avatar asked May 27 '14 10:05

Keith


People also ask

What is hist () with parameters?

The hist() function in pyplot module of matplotlib library is used to plot a histogram. Parameters: This method accept the following parameters that are described below: x : This parameter are the sequence of data. bins : This parameter is an optional parameter and it contains the integer or sequence or string.

How do I normalize a histogram in Matplotlib?

We can normalize a histogram in Matplotlib using the density keyword argument and setting it to True . By normalizing a histogram, the sum of the bar area equals 1.

How do you set a range in a histogram in Python?

To create a histogram the first step is to create bin of the ranges, then distribute the whole range of the values into a series of intervals, and count the values which fall into each of the intervals. Bins are clearly identified as consecutive, non-overlapping intervals of variables. The matplotlib. pyplot.

What does the function hist return in Matplotlib?

The function returns a tuple that contains the frequencies of the histogram bins, the edges of the bins, and the respective patches that create the histogram.

How to modify a histogram using matplotlib Hist () function?

matplotlib.pyplot.hist () function itself provides many attributes with the help of which we can modify a histogram.The hist () function provide a patches object which gives access to the properties of the created objects, using this we can modify the plot according to our will. Example 1: import matplotlib.pyplot as plt import numpy as np

How to get a plot with manually selected limits in Matplotlib?

As seen in the output, we would get a plot with the complete range of axes, with the X-axis ranging from 0 to 80 and the Y-axis ranging from 0 to 50. Example #2 In this example, we use set_xlim () and set_ylim () functions, to get a plot with manually selected limits. Import matplotlib.pyplot library. After this, import the numpy package.

How to set axes limits of log scale plot in Matplotlib?

Here we use xlim () and ylim () methods to set axes limits of log scale plot. Import matplotlib.pyplot library for data visualization. Next, define data coordinates. To set x-axis scale to log, use xscale () function and pass log to it. To plot the graph, use plot () function.

How to set the axis range of the datetime plot using matplotlib?

Here, we’ll learn to set the axis range of the datetime plot using matplotlib. Import necessary libraries such as datetime, and matplotlib.pyplot. To create a subplot, use subplots () function. To define data coordinates, use datetime.date () function. To set axes, use set_xlim () and set_ylim () functions.


1 Answers

Actually, it works if you specify with range an interval shorter than [-100, 100]. For example, this work :

import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform(-100, 100, 1000)
plt.hist(x, bins=30, range=(-50, 50))
plt.show()

If you want to plot the histogram on a range larger than [x.min(), x.max()] you can change xlim propertie of the plot.

import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform(-100, 100, 1000)
plt.hist(x, bins=30)
plt.xlim(-500, 500)
plt.show()
like image 180
Ger Avatar answered Oct 08 '22 15:10

Ger