Добавил:
KaFaka
t.me
Инфо для ГУАП студентов от меня: https://kafaka.notion.site/99e6d9b70ca74f7baef3daea17839e5a
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:4 курс 1 семестр / Курсовая / Python / BD_KR1
.py import matplotlib.pyplot as plt
import mysql.connector
# Connect to the database
cnx = mysql.connector.connect(
user='root',
password='admin',
host='127.0.0.1',
port=3306,
database='airport')
# Create a cursor object
cursor = cnx.cursor()
# execute a query
query = '''
SELECT des_city, COUNT(*) as popularity
FROM flights
GROUP BY des_city
'''
cursor.execute(query)
# fetch the results
results = cursor.fetchall()
# print the results
print(results)
# extract the data from results
cities = [r[0] for r in results]
popularity = [r[1] for r in results]
# create a bar chart
plt.bar(cities, popularity)
plt.grid()
plt.xlabel('Города прибытия')
plt.ylabel('Популярность')
plt.show()
# Close the cursor and the connection
cursor.close()
cnx.close()
import mysql.connector
# Connect to the database
cnx = mysql.connector.connect(
user='root',
password='admin',
host='127.0.0.1',
port=3306,
database='airport')
# Create a cursor object
cursor = cnx.cursor()
# execute a query
query = '''
SELECT des_city, COUNT(*) as popularity
FROM flights
GROUP BY des_city
'''
cursor.execute(query)
# fetch the results
results = cursor.fetchall()
# print the results
print(results)
# extract the data from results
cities = [r[0] for r in results]
popularity = [r[1] for r in results]
# create a bar chart
plt.bar(cities, popularity)
plt.grid()
plt.xlabel('Города прибытия')
plt.ylabel('Популярность')
plt.show()
# Close the cursor and the connection
cursor.close()
cnx.close()