Sunday, 2 March 2025

Honeycomb pattern plot using python

 



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

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (247) Cybersecurity (25) Data Analysis (1) Data Analytics (2) data management (11) Data Science (142) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (9) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (78) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1012) Python Coding Challenge (452) Python Quiz (91) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses

Python Coding for Kids ( Free Demo for Everyone)