Commonly used python hist histogram code

 

The following is a simple Python code example for drawing a histogram:
```python
import matplotlib.pyplot as plt
import numpy as np
# Generate random data
data = np.random.randn(1000)
# Draw a histogram
plt.hist (data, bins=30, density=True, alpha=0.5, color='blue')
# Add title and label
plt.title('Histogram of Random Data')
plt.xlabel('Value')
plt.ylabel(' Frequency')
# Display graphics
plt.show()
```
This code uses the Matplotlib library to draw histograms. `np.random.randn(1000)` generates 1000 random numbers, and the `plt.hist()` function is used to draw a histogram, where the `bins` parameter specifies the number of columns in the histogram, and the `density` parameter specifies Whether to normalize the histogram, the `alpha` parameter specifies the transparency of the column, and the `color` parameter specifies the color of the column. Finally, use the `plt.title()`, `plt.xlabel()`, and `plt.ylabel()` functions to add a title and label, and use the `plt.show()` function to display the graph.

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130694929