8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [227]:
import matplotlib.pyplot as plt
In [228]:
import numpy as np
import pandas as pd
In [246]:
data = pd.read_csv(r"C:\Users\surya\OneDrive\Desktop\DemographicData.csv")
data
Out[246]:
CountryName CountryCode BirthRate InternetUsers IncomeGroup
0 Aruba ABW 10.244 78.9 High income
1 Afghanistan AFG 35.253 5.9 Low income
2 Angola AGO 45.985 19.1 Upper middle income
3 Albania ALB 12.877 57.2 Upper middle income
4 United Arab Emirates ARE 11.044 88.0 High income
... ... ... ... ... ...
190 Yemen, Rep. YEM 32.947 20.0 Lower middle income
191 South Africa ZAF 20.850 46.5 Upper middle income
192 Congo, Dem. Rep. COD 42.394 2.2 Low income
193 Zambia ZMB 40.471 15.4 Lower middle income
194 Zimbabwe ZWE 35.715 18.5 Low income
195 rows × 5 columns
In [247]:
x = data["CountryName"].head()
x
Out[247]:
0 Aruba
1 Afghanistan
2 Angola
3 Albania
4 United Arab Emirates
Name: CountryName, dtype: object
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 1/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [248]:
y = data["BirthRate"].head()
y
Out[248]:
0 10.244
1 35.253
2 45.985
3 12.877
4 11.044
Name: BirthRate, dtype: float64
In [249]:
p = data["InternetUsers"].head()
p
Out[249]:
0 78.9
1 5.9
2 19.1
3 57.2
4 88.0
Name: InternetUsers, dtype: float64
In [250]:
i= data["IncomeGroup"].head()
i
Out[250]:
0 High income
1 Low income
2 Upper middle income
3 Upper middle income
4 High income
Name: IncomeGroup, dtype: object
In [251]:
data.shape
Out[251]:
(195, 5)
Matplotlib
Matplotlib is a popular data visualization library for Python. It provides a wide range of plotting functions to
create different types of
plots. Here are some of the most commonly used types of plots in Matplotlib
1. Line plot: This is a basic plot that shows the relationship between two variables, usually with time on the
x-axis and values on the
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 2/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
y-axis
2. Scatter plot: This plot displays the relationship between two variables as a collection of points, with one
variable on the
the other variable on the y-axis and x-axis
3. Histogram: This is a plot that shows the distribution of a single variable by dividing it into a set of bins
and displaying the count or
frequency of each bin as a bar
4. Bar plot This plot shows the comparison of a set of discrete data points, usually represented as bars.
5. Pie chart This plot displays the proportions of each category in a dataset as slices of a pie. 6. Box plot
This plot displays the distribution of a set of data by showing the median, quartiles, and outliers,
6. Heat map: This plot displays data values as colors in a grid, with each cell representing a data point
7. Contour plot This is a two-dimensional plot that displays the contours of a three-dimensional surface.
8. 3D plot This plot displays data in a three-dimensional space. 10.Polar plot: This plot displays data in
polar coordinates, where the
angle and radius represent the variables
Scatter Plot:
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 3/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [252]:
plt.scatter(x, y, marker='o', color='blue', label='Data')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot')
plt.grid(True)
plt.legend()
plt.show()
#Same as the Line Plot, with an additional color parameter.
#Purpose: Represents individual data points as dots. Useful for visualizing data distribu
#Use Case: Plotting data points like exam scores, population vs. GDP, or any other two-d
Line Plot
#x and y: Lists of data points for the x and y axes. #marker: Marker style for data points (e.g., 'o' for circles).
#label: Label for the data series, used for the legend. #xlabel, ylabel: Labels for the x and y axes. #title: Title
of the plot. #grid: Display grid lines (True or False).
#Purpose: Displays a line connecting data points. Useful for showing trends over a continuous range. #Use
Case: Visualizing trends in stock prices, temperature changes over time, or any continuous data.
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 4/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [253]:
plt.plot(x,y, marker='o', label='Data')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line Plot')
plt.grid(True)
plt.legend()
plt.show()
Bar Plot:
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 5/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [254]:
plt.bar(x, y, color='green', label='Data')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Plot')
plt.legend()
plt.show()
#categories: List of category labels.
#values: List of corresponding values.
#label: Label for the data series, used for the legend.
#Purpose: Displays bars representing data in distinct categories. Useful for comparing qu
#Use Case: Visualizing sales by product category, survey results, or any categorical data
Box Plot
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 6/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [265]:
data = np.random.randn(5, 10)
plt.boxplot(data)
plt.xlabel('Data')
plt.ylabel('Value')
plt.title('Box Plot')
plt.show()
#data: Array of values to create the box plot.
#Purpose: Displays the distribution of data, including median, quartiles, and potential o
#Use Case: Analyzing data distributions in different groups or datasets.
In [266]:
z = np.random.rand(3, 3)
z
Out[266]:
array([[0.48152196, 0.92931064, 0.33114952],
[0.42202644, 0.58324458, 0.10559429],
[0.7508846 , 0.85712153, 0.46178022]])
Heat Map
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 7/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [267]:
#data = np.random.rand(10, 10)
plt.imshow(data, cmap='coolwarm', interpolation='nearest')
plt.colorbar()
plt.title('Heatmap')
plt.show()
#data: 2D array of values to create the heatmap.
#cmap: Colormap for representing data values.
#Purpose: Displays a color-coded representation of data values in a matrix. Useful for v
#Use Case: Analyzing relationships between variables, visualizing sensor data, or display
Stacked Bar Plot:
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 8/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [268]:
categories = ['A', 'B', 'C', 'D']
values1 = [20, 35, 30, 25]
values2 = [15, 20, 10, 30]
plt.bar(categories, values1, color='blue', label='Data 1')
plt.bar(categories, values2, color='orange', bottom=values1, label='Data 2')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Stacked Bar Plot')
plt.legend()
plt.show()
#bottom: List of heights of the bars to be stacked on.
#label: Label for the data series, used for the legend.
#Purpose: Displays stacked bars to compare contributions of multiple data series within
#Use Case: Visualizing revenue breakdown by product categories and regions.
Area Plot (Stacked Line Plot):
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 9/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [269]:
x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
y2 = [1, 3, 5, 7, 9]
plt.stackplot(x, y1, y2, labels=['Data 1', 'Data 2'], alpha=0.5)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Stacked Area Plot')
plt.legend(loc='upper left')
plt.show()
#labels: Labels for the data series, used for the legend.
#alpha: Transparency of the plot.
#Purpose: Shows the cumulative contribution of multiple data series over a continuous ran
#Use Case: Tracking progress of different teams in a project over time.
Violin Plot:
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 10/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [270]:
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.violinplot(data, showmedians=True)
plt.xlabel('Distribution')
plt.ylabel('Value')
plt.title('Violin Plot')
plt.xticks(np.arange(1, len(data) + 1), ['Data 1', 'Data 2', 'Data 3'])
plt.legend(['Median'])
plt.show()
#showmedians: Display medians in the plot.
#Purpose: Combines a box plot with a kernel density estimation to show the distribution o
#Use Case: Comparing distributions of exam scores across different subjects.
3D Surface Plot:
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 11/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [271]:
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
ax.set_title('3D Surface Plot')
plt.show()
#cmap: Colormap for representing data values.
#Purpose: Displays a three-dimensional surface plot based on data values.
#Use Case: Visualizing complex mathematical functions or terrain elevation dat
In [ ]:
In [ ]:
In [ ]:
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 12/13
8/11/23, 12:30 AM Untitled - Jupyter Notebook
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
localhost:8889/notebooks/Downloads/Untitled.ipynb?kernel_name=python3#Matplotlib 13/13