matplotlib hist
时间: 2023-10-14 20:24:12 浏览: 83
Matplotlib hist is a function in the Matplotlib library used to create histograms. A histogram is a graphical representation of the distribution of data. It shows the frequency of occurrences of a variable within a given range. The hist function takes an array of data and plots the frequency of the values within a specified number of bins.
The syntax for the Matplotlib hist function is:
```python
matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, *, data=None, **kwargs)
```
- `x`: This is the input data array.
- `bins`: This specifies the number of bins to use in the histogram. If not specified, the default value is 10.
- `range`: This specifies the range of the data to be plotted.
- `density`: This is a boolean parameter that determines whether to normalize the histogram or not.
- `color`: This specifies the color of the bars in the histogram.
- `label`: This is used to label the histogram.
- `histtype`: This specifies the type of histogram to be plotted, such as ‘bar’, ‘step’, or ‘stepfilled’.
- `orientation`: This specifies the orientation of the bars in the histogram, such as ‘horizontal’ or ‘vertical’.
Here is an example of how to use the Matplotlib hist function:
```python
import matplotlib.pyplot as plt
import numpy as np
# Generate some random data
data = np.random.normal(size=1000)
# Create a histogram using Matplotlib hist
plt.hist(data, bins=30, density=True, color='green', alpha=0.5)
# Add labels and title
plt.xlabel('Data')
plt.ylabel('Frequency')
plt.title('Histogram of Random Data')
# Show the plot
plt.show()
```
This will create a histogram of 1000 randomly generated data points with 30 bins, normalized to a density plot, in green color with an alpha value of 0.5. The plot will also have x and y labels as well as a title.
阅读全文