Tuesday, 25 March 2025

Barcode Like Pattern using Python

 


import matplotlib.pyplot as plt

import numpy as np

np.random.seed(42)

num_bars=50

bar_positions=np.arange(num_bars)

bar_widths=np.random.choice([0.1,0.2,0.3,0.4],size=num_bars)

bar_heights=np.ones(num_bars)

fig,ax=plt.subplots(figsize=(10,5))

ax.bar(bar_positions,bar_heights,width=bar_widths,color="black")

ax.set_xticks([])

ax.set_yticks([])

ax.spines['top'].set_visible(False)

ax.spines['bottom'].set_visible(False)

ax.spines['left'].set_visible(False)

ax.spines['right'].set_visible(False)

plt.title("Barcode like pattern")

plt.show()

#source code --> clcoding.com

Code Explanation:

1. Import Required Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy: Used to generate random numbers and create an array of positions.

matplotlib.pyplot: Used to create and display the plot.

 2. Set Random Seed for Reproducibility

np.random.seed(42) 

Ensures the same random numbers are generated every time the code runs.

 3. Define Number of Bars

num_bars = 50

Specifies 50 vertical bars to be plotted.

 4. Define Bar Positions

bar_positions = np.arange(num_bars)

Creates an array [0, 1, 2, ..., 49] to specify the position of each bar along the x-axis.

 5. Generate Random Bar Widths

bar_widths = np.random.choice([0.1, 0.2, 0.3, 0.4], size=num_bars)

Randomly selects bar widths from [0.1, 0.2, 0.3, 0.4] for each bar, adding variation.

 6. Define Uniform Bar Heights

bar_heights = np.ones(num_bars)

Sets all bars to the same height of 1 (uniform height).

 7. Create the Figure and Axis

fig, ax = plt.subplots(figsize=(10, 5))

Creates a figure (fig) and an axis (ax) with a 10x5 aspect ratio for better visibility.

 8. Plot the Bars

ax.bar(bar_positions, bar_heights, width=bar_widths, color='black')

Plots the bars at bar_positions with:

bar_heights = 1 (uniform height)

bar_widths (randomly chosen for variation)

Black color, to resemble a barcode.

 9. Remove X and Y Ticks

ax.set_xticks([])

ax.set_yticks([])

Hides x-axis and y-axis ticks to give a clean barcode appearance.

 10. Remove Borders for a Clean Look

ax.spines['top'].set_visible(False)

ax.spines['right'].set_visible(False)

ax.spines['left'].set_visible(False)

ax.spines['bottom'].set_visible(False)

Hides the top, right, left, and bottom borders (spines) of the plot.

 11. Display the Plot

plt.show()

Renders and displays the barcode-like pattern.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (39) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (197) C (77) C# (12) C++ (83) Course (67) Coursera (249) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (148) 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 (11) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (85) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1041) Python Coding Challenge (454) Python Quiz (116) 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

Python Coding for Kids ( Free Demo for Everyone)