Friday, 29 August 2025

Python chart

 #ex1
import matplotlib.pyplot as plt
names = []
marks = []

f = open('test1.txt','r')
for row in f:
    row = row.split(' ')
    names.append(row[0])
    marks.append(int(row[1]))

plt.bar(names, marks, color = 'g', label = 'File Data')

plt.xlabel('Student Names', fontsize = 12)
plt.ylabel('Marks', fontsize = 12)

plt.title('Students Marks', fontsize = 20)
plt.legend()
plt.show()
------------------
#ex2
import matplotlib.pyplot as plt

names = []
work = []

for line in open('test1.txt', 'r'):
    Data = [i for i in line.split()]
    names.append(Data[0])
    New_Data= [ j for j in Data[1].split('%')]
    
    work.append(New_Data[0])
colors = ['yellow', 'b', 'green', 'cyan','red'] 
  
# plotting pie chart 
plt.pie(work, labels = names, colors = colors, startangle = 90,
        shadow = True, radius = 1.2, autopct = '%1.1f%%') 
plt.show()
------------------------
#ex3
import matplotlib.pyplot as plt
x = []
y = []
for line in open('test1.txt', 'r'):
    lines = [i for i in line.split()]
    x.append(lines[0])
    y.append(int(lines[1]))
    
plt.title("Students Marks")
plt.xlabel('Name')
plt.ylabel('Marks')
plt.yticks(y)
plt.plot(x, y, marker = 'o', c = 'g')

plt.show()
--------------------------
#ex4
import pandas as pd
import matplotlib.pyplot as plt

# Read data from a CSV file
df = pd.read_csv('Book1.csv')

# Create a bar chart
df.plot(kind='bar', x='Name', y='Marks')
plt.xlabel('Name')
plt.ylabel('Marks')
plt.title('Bar Chart Example')
plt.show()

# Create a scatter plot
df.plot(kind='scatter', x='x_values', y='y_values')
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Scatter Plot Example')
plt.show()
---------------------------------------------------
import mysql.connector
import pandas as pd
import matplotlib.pyplot as plt

# Replace with your MySQL credentials
mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="jahab"
)

# Read data into DataFrame
query = "SELECT name, marks FROM student"
df = pd.read_sql(query, mydb)

# Create a bar chart
plt.figure(figsize=(10, 6))
plt.bar(df['name'], df['marks'])
plt.xlabel('Name')
plt.ylabel('Marks')
plt.title('Student Marks')
plt.show()

# Close the database connection
mydb.close()


No comments:

Post a Comment

Slide - CSS

   <html> <head> <style> /* Slideshow container */ .slideshow-container {   max-width: 1000px;   position: relative;   mar...