Python Unit 5
Python Unit 5
Q) Data Visualization using Matplotlib in Python or Explain different type of charts in Python
Matplotlib is a powerful and widely-used Python library for creating static, animated and interactive data
visualizations. In this article, we will provide a guide on Matplotlib and how to use it for data visualization with
practical implementation. Matplotlib supports a variety of plots including line charts, bar charts, histograms,
scatter plots, etc. Let’s understand them with implementation using pyplot.
1. Line Chart
Line chart is one of the basic plots and can be created using the plot() function. It is used to represent a
relationship between two data X and Y on a different axis.
Example:
import matplotlib.pyplot as plt
x = [10, 20, 30, 40]
y = [20, 25, 35, 55]
plt.plot(x, y)
plt.title("Line Chart")
plt.ylabel('Y-Axis')
plt.xlabel('X-Axis')
plt.show()
2. Bar Chart
A bar chart is a graph that represents the category of data with rectangular bars with lengths and heights that is
proportional to the values which they represent. The bar plots can be plotted horizontally or vertically. A bar
chart describes the comparisons between the different categories. It can be created using the bar() method.
In the below example we will use the tips dataset. Tips database is the record of the tip given by the customers
in a restaurant for two and a half months in the early 1990s. It contains 6 columns as total_bill, tip, sex, smoker,
day, time, size.
Example:
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('tips.csv')
x = data['day']
y = data['total_bill']
plt.bar(x, y)
plt.title("Tips Dataset")
plt.ylabel('Total Bill')
plt.xlabel('Day')
plt.show()
3. Histogram:-A histogram is basically used to represent data provided in a form of some groups. It is a type of
bar plot where the X-axis represents the bin ranges while the Y-axis gives information about frequency.
The hist() function is used to compute and create histogram of x.
Example:
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('tips.csv')
x = data['total_bill']
plt.hist(x)
plt.title("Tips Dataset")
plt.ylabel('Frequency')
plt.xlabel('Total Bill')
plt.show()
4. Scatter Plot:-Scatter plots are used to observe relationships between variables. The scatter() method in the
matplotlib library is used to draw a scatter plot.
Example:
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('tips.csv')
x = data['day']
y = data['total_bill']
plt.scatter(x, y)
plt.title("Tips Dataset")
plt.ylabel('Total Bill')
plt.xlabel('Day')
plt.show()
5. Pie Chart:-Pie chart is a circular chart used to display only one series of data. The area of slices of the pie
represents the percentage of the parts of the data. The slices of pie are called wedges. It can be created using
the pie() method.
Syntax:
matplotlib.pyplot.pie(data, explode=None, labels=None, colors=None, autopct=None, shadow=False)
Example:
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('tips.csv')
cars = ['AUDI', 'BMW', 'FORD', 'TESLA', 'JAGUAR',]
data = [23, 10, 35, 15, 12]
plt.pie(data, labels=cars)
plt.title("Car data")
plt.show()
Python3
# Python program to connect
# to mysql database
import mysql.connector
# Connecting from the server
conn = mysql.connector.connect(user = 'username',
host = 'localhost',
database = 'database_name')
print(conn)
# Disconnecting from the server
conn.close()
Select Query
After connecting with the database in MySQL we can select queries from the tables in it. Syntax:
In order to select particular attribute columns from a table, we write the attribute names.
SELECT attr1, attr2 FROM table_name
In order to select all the attribute columns from a table, we use the asterisk ‘*’ symbol.
SELECT * FROM table_name