Добавил:
t.me Инфо для ГУАП студентов от меня: https://kafaka.notion.site/99e6d9b70ca74f7baef3daea17839e5a Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
11
Добавлен:
24.10.2023
Размер:
1.09 Кб
Скачать
import mysql.connector
import matplotlib.pyplot as plt

# Connect to the database
cnx = mysql.connector.connect(
host='127.0.0.1',
user='root',
password='admin',
database='airport'
)

# Create a cursor object
cursor = cnx.cursor()

# Define the query
query = "SELECT dep_city, des_city, count(*) as flight_count FROM flights GROUP BY dep_city, des_city ORDER BY flight_count DESC"

# Execute the query
cursor.execute(query)

# Fetch the results
results = cursor.fetchall()

# Print the results
for result in results:
print(result)

# Close the cursor and connection
cursor.close()
cnx.close()

# Get the top 10 results
top_5_results = results[:5]

# Extract the labels and values for the chart
labels = [f"{result[0]}-{result[1]}" for result in top_5_results]
values = [result[2] for result in top_5_results]

# Create the bar chart
plt.bar(labels, values)
plt.xlabel("Departure-Destination City Pair")
plt.ylabel("Flight Count")
plt.title("Top 5 Departure-Destination City Pairs by Flight Count")

# Show the chart
plt.show()
Соседние файлы в папке Python