📘 5种高级可视化图表/六边形分箱图 (Hexbin Plot).ipynb

Notebook
import numpy as np import matplotlib.pyplot as plt from mplhexbin import HexBin # Simulated data np.random.seed(0) # Ensure reproducibility n_points = 10000 x = np.random.rand(n_points) * 100 # Air Quality Index (AQI) range from 0 to 100 y = 5 * np.sin(x * np.pi / 50) + np.random.randn(n_points) * 15 # Simulated hospital visits, related to AQI but with noise # Create a new figure fig, ax = plt.subplots(figsize=(10, 8)) # Use HexBin to create a hexagonal bin plot hb = HexBin(ax, gridsize=20, cmap='viridis', extent=[0, 100, -30, 50]) # Set grid size, colormap, and range hb.hexbin(x, y, mincnt=1) # Draw the hexagonal bin plot, mincnt sets the minimum count threshold # Add title and axis labels ax.set_title('Relationship between Air Quality Index (AQI) and Hospital Visits') ax.set_xlabel('Air Quality Index (AQI)') ax.set_ylabel('Hospital Visits') # Show the figure plt.colorbar(hb.cmap, ax=ax, label='Number of Data Points') # Add color bar and set label plt.show()