import matplotlib.pyplot as plt
import numpy as np
x=np.random.randn(10000)
y=np.random.randn(10000)
plt.hexbin(x,y,gridsize=30,cmap='Blues',edgecolor='gray')
plt.colorbar(label='Count in bin')
plt.title('Honeycomb pattern plot')
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Import Required Libraries
import matplotlib.pyplot as plt
import numpy as np
matplotlib.pyplot is used for plotting.
numpy is used for numerical operations like generating random data.
2. Generate Random Data
x = np.random.randn(10000)
y = np.random.randn(10000)
np.random.randn(10000) generates 10,000 random numbers from a normal distribution (mean = 0, standard deviation = 1).
Two sets of such numbers are stored in x and y, forming random (x, y) pairs.
3. Create a Hexbin Plot
plt.hexbin(x, y, gridsize=30, cmap='Blues', edgecolor='gray')
hexbin(x, y, gridsize=30, cmap='Blues', edgecolor='gray'):
gridsize=30: Specifies the number of hexagonal bins in the grid (higher value = smaller hexagons).
cmap='Blues': Uses the "Blues" colormap for coloring the hexagons based on data density.
edgecolor='gray': Adds gray borders around hexagons for better visibility.
4. Add a Color Bar
plt.colorbar(label='Count in bin')
plt.colorbar() adds a color scale bar.
label='Count in bin' describes the color bar as representing the number of points inside each hexagon.
5. Customize the Plot
plt.title('Honeycomb pattern plot')
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
Adds title and axis labels.
6. Display the Plot
plt.show()
Renders the plot.
0 Comments:
Post a Comment