matplotlib hist2d
时间: 2023-08-17 20:06:11 浏览: 122
Matplotlib's `hist2d` function is used to generate a 2D histogram plot. It takes in two arrays of data, representing the x and y coordinates, and creates a 2D histogram with bins on both axes.
Here's an example of how to use `hist2d`:
```python
import matplotlib.pyplot as plt
import numpy as np
# Generate some random data
x = np.random.randn(1000)
y = np.random.randn(1000)
# Create the 2D histogram
plt.hist2d(x, y, bins=20, cmap='Blues')
# Add labels and a colorbar
plt.xlabel('X')
plt.ylabel('Y')
plt.colorbar()
# Show the plot
plt.show()
```
In this example, `x` and `y` are arrays of random data. We pass these arrays to `hist2d` along with the number of bins (20 in this case) and a color map (`'Blues'` in this case) to specify the color scheme of the plot. We then add labels for the x and y axes and a colorbar to indicate the color scale. Finally, we call `plt.show()` to display the plot.
The resulting plot will show a 2D histogram, with the x-axis representing the values in `x`, the y-axis representing the values in `y`, and the color indicating the frequency of occurrence in each bin.
阅读全文