0% found this document useful (0 votes)
16 views21 pages

Matplotlib Plot Types Explained

The document provides an introduction to Matplotlib, a Python library for creating various types of plots and visualizations. It outlines different plot types, including line plots, scatter plots, bar plots, and more, explaining their uses and characteristics. The document also includes examples of how to create a line plot using Matplotlib.

Uploaded by

aashishdrb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views21 pages

Matplotlib Plot Types Explained

The document provides an introduction to Matplotlib, a Python library for creating various types of plots and visualizations. It outlines different plot types, including line plots, scatter plots, bar plots, and more, explaining their uses and characteristics. The document also includes examples of how to create a line plot using Matplotlib.

Uploaded by

aashishdrb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Subject: Introduction to python programming

Branch: Computer Science Engineering

Subject Teacher: Ramakant Soni, Assistant Professor(CSE Dept.), BKBIET Pilani

Introduction to Matplotlib:
Matplotlib is a popular Python library used for creating a wide range of high-quality plots and visualizations.

It provides a flexible and straightforward interface for generating various plot types and customizing them
to suit specific requirements.

In this lesson, we'll explore different types of plots and various essential features of Matplotlib.

Different Plot types :

1. Line Plot: A line plot represents data points connected by straight lines, often used to visualize trends
or continuous data.

2. Scatter Plot: Scatter plots display individual data points as dots, suitable for visualizing the relationship
between two variables.

3. Bar Plot: Bar plots use rectangular bars to represent data, commonly used for comparing categorical
data.

4. Histogram: Histograms show the distribution of continuous data by grouping data into bins and
displaying their frequency.

5. Box Plot: Box plots display the distribution of data using quartiles, useful for visualizing the spread and
outliers in data.

6. Pie Chart: Pie charts represent data as slices of a circle, showing the proportion of different categories
to the whole.

7. Area Plot: Area plots display data as filled regions, showing how the data changes over time or
categories.

8. Stacked Bar Chart: Stacked bar charts display multiple datasets stacked on top of each other,
emphasizing the total.

9. Stack Plot: Stack plots show how different datasets contribute to the total, useful for visualizing
cumulative data.

10. 3D Plot: 3D plots represent data in three-dimensional space, suitable for visualizing complex
relationships.

11. Polar Plot: Polar plots are used to visualize data in polar coordinates, often used for cyclic data.

12. Contour Plot: Contour plots display 2D data as continuous curves, showing regions with similar values.
13. Heatmap: Heatmaps visualize 2D data as a grid of colors, used to represent data intensity or
correlation.

14. Hexbin Plot: Hexbin plots represent data density using hexagonal bins, useful for visualizing large
datasets.

15. Violin Plot: Violin plots combine box plots with a density plot, providing more information about data
distribution.

16. Bubble Chart: Bubble charts show data points as circles, with the size of each circle representing a third
variable.

17. Error Bars: Error bars show uncertainty or variability in data points, used to indicate measurement
errors or confidence intervals.

18. Quiver Plot: Quiver plots display 2D vector fields using arrows, representing the magnitude and
direction of each vector.

19. Stream Plot: Stream plots visualize vector fields as streamlines, showing the flow of fluid or particles.

20. Density Plot: Density plots display the density of data points in a 2D space, providing insights into data
distribution.

Each plot type serves different purposes and is suitable for different data types and visualizations.
Understanding these plot types allows you to choose the most appropriate visualization method for your
data analysis and presentation.

1. Line Plot:
A line plot is a type of plot that displays data points connected by straight lines. It is commonly used to
visualize trends over time or continuous data.

Example: Line Plot using Matplotlib:

In [2]: import [Link] as plt

x = [1, 2, 3, 4, 5]
y = [10, 25, 18, 30, 22]

[Link](x, y)
[Link]()
In [4]: import [Link] as plt
import numpy as np
#x = [1, 2, 3, 4, 5]
#y = [10, 25, 18, 30, 22]

x=[Link](1,500,10)
y=x**2
z=x**3
l=x
[Link](x, y, marker='^', linestyle='-', color='g',label='X^2 Data Points')
[Link](x, z, marker='o', markerfacecolor='blue',linestyle='--', color='r', label='x^3
[Link](x, l, marker='o', linestyle='-', color='y', label='x Data Points')
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Line Plot Example')
[Link]()

[Link]()
In [32]: x
array([ 1. , 1.1638796 , 1.3277592 , 1.4916388 , 1.65551839,
Out[32]:
1.81939799, 1.98327759, 2.14715719, 2.31103679, 2.47491639,
2.63879599, 2.80267559, 2.96655518, 3.13043478, 3.29431438,
3.45819398, 3.62207358, 3.78595318, 3.94983278, 4.11371237,
4.27759197, 4.44147157, 4.60535117, 4.76923077, 4.93311037,
5.09698997, 5.26086957, 5.42474916, 5.58862876, 5.75250836,
5.91638796, 6.08026756, 6.24414716, 6.40802676, 6.57190635,
6.73578595, 6.89966555, 7.06354515, 7.22742475, 7.39130435,
7.55518395, 7.71906355, 7.88294314, 8.04682274, 8.21070234,
8.37458194, 8.53846154, 8.70234114, 8.86622074, 9.03010033,
9.19397993, 9.35785953, 9.52173913, 9.68561873, 9.84949833,
10.01337793, 10.17725753, 10.34113712, 10.50501672, 10.66889632,
10.83277592, 10.99665552, 11.16053512, 11.32441472, 11.48829431,
11.65217391, 11.81605351, 11.97993311, 12.14381271, 12.30769231,
12.47157191, 12.63545151, 12.7993311 , 12.9632107 , 13.1270903 ,
13.2909699 , 13.4548495 , 13.6187291 , 13.7826087 , 13.94648829,
14.11036789, 14.27424749, 14.43812709, 14.60200669, 14.76588629,
14.92976589, 15.09364548, 15.25752508, 15.42140468, 15.58528428,
15.74916388, 15.91304348, 16.07692308, 16.24080268, 16.40468227,
16.56856187, 16.73244147, 16.89632107, 17.06020067, 17.22408027,
17.38795987, 17.55183946, 17.71571906, 17.87959866, 18.04347826,
18.20735786, 18.37123746, 18.53511706, 18.69899666, 18.86287625,
19.02675585, 19.19063545, 19.35451505, 19.51839465, 19.68227425,
19.84615385, 20.01003344, 20.17391304, 20.33779264, 20.50167224,
20.66555184, 20.82943144, 20.99331104, 21.15719064, 21.32107023,
21.48494983, 21.64882943, 21.81270903, 21.97658863, 22.14046823,
22.30434783, 22.46822742, 22.63210702, 22.79598662, 22.95986622,
23.12374582, 23.28762542, 23.45150502, 23.61538462, 23.77926421,
23.94314381, 24.10702341, 24.27090301, 24.43478261, 24.59866221,
24.76254181, 24.9264214 , 25.090301 , 25.2541806 , 25.4180602 ,
25.5819398 , 25.7458194 , 25.909699 , 26.0735786 , 26.23745819,
26.40133779, 26.56521739, 26.72909699, 26.89297659, 27.05685619,
27.22073579, 27.38461538, 27.54849498, 27.71237458, 27.87625418,
28.04013378, 28.20401338, 28.36789298, 28.53177258, 28.69565217,
28.85953177, 29.02341137, 29.18729097, 29.35117057, 29.51505017,
29.67892977, 29.84280936, 30.00668896, 30.17056856, 30.33444816,
30.49832776, 30.66220736, 30.82608696, 30.98996656, 31.15384615,
31.31772575, 31.48160535, 31.64548495, 31.80936455, 31.97324415,
32.13712375, 32.30100334, 32.46488294, 32.62876254, 32.79264214,
32.95652174, 33.12040134, 33.28428094, 33.44816054, 33.61204013,
33.77591973, 33.93979933, 34.10367893, 34.26755853, 34.43143813,
34.59531773, 34.75919732, 34.92307692, 35.08695652, 35.25083612,
35.41471572, 35.57859532, 35.74247492, 35.90635452, 36.07023411,
36.23411371, 36.39799331, 36.56187291, 36.72575251, 36.88963211,
37.05351171, 37.2173913 , 37.3812709 , 37.5451505 , 37.7090301 ,
37.8729097 , 38.0367893 , 38.2006689 , 38.36454849, 38.52842809,
38.69230769, 38.85618729, 39.02006689, 39.18394649, 39.34782609,
39.51170569, 39.67558528, 39.83946488, 40.00334448, 40.16722408,
40.33110368, 40.49498328, 40.65886288, 40.82274247, 40.98662207,
41.15050167, 41.31438127, 41.47826087, 41.64214047, 41.80602007,
41.96989967, 42.13377926, 42.29765886, 42.46153846, 42.62541806,
42.78929766, 42.95317726, 43.11705686, 43.28093645, 43.44481605,
43.60869565, 43.77257525, 43.93645485, 44.10033445, 44.26421405,
44.42809365, 44.59197324, 44.75585284, 44.91973244, 45.08361204,
45.24749164, 45.41137124, 45.57525084, 45.73913043, 45.90301003,
46.06688963, 46.23076923, 46.39464883, 46.55852843, 46.72240803,
46.88628763, 47.05016722, 47.21404682, 47.37792642, 47.54180602,
47.70568562, 47.86956522, 48.03344482, 48.19732441, 48.36120401,
48.52508361, 48.68896321, 48.85284281, 49.01672241, 49.18060201,
49.34448161, 49.5083612 , 49.6722408 , 49.8361204 , 50. ])

[Link]() Parameters:

Required parameters:

x: The x-coordinates of the data points.


y: The y-coordinates of the data points.

Optional parameters:

marker: The marker style used for the data points.


linestyle: The line style used for connecting the data points.
color: The color of the line and data points.
label: The label for the plot, used for creating a legend.

In [1]: import [Link] as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 6, 8]

# Create a line plot


[Link](x, y, label='Line Plot', color='blue', marker='o', linestyle='-', linewidth=2, m
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Line Plot Example')
[Link]()
[Link](True)
[Link]()
In [10]: import pandas as pd
import [Link] as plt

# Sample data
data = {
'Year': [2010, 2011, 2012, 2013, 2014, 2015],
'Revenue': [5000, 6000, 7500, 8000, 9000, 10000],
'Profit': [1000, 1200, 1500, 1600, 1800, 2000]
}

df = [Link](data)

# Create a line plot


ax = [Link](x='Year', y='Revenue', kind='line', title='Revenue Over Years',
color='b', marker='o', markerfacecolor='red', markeredgecolor='blue',linest
linewidth=2, markersize=8, legend=True, grid=True)

# Add a second line to the same plot


[Link](x='Year', y='Profit', kind='line', ax=ax, color='g', marker='s', linestyle='--',

# Customize labels and legend


[Link]('Year')
[Link]('Amount (in $)')
[Link](['Revenue', 'Profit'])

[Link]()
In [ ]:

In [23]:
In [24]: import pandas as pd
import [Link] as plt
from [Link] import display

# Sample data
data = {
'Year': [2010, 2011, 2012, 2013, 2014, 2015],
'Revenue': [5000, 6000, 7500, 8000, 9000, 10000],
}

df = [Link](data)

# Create a line plot with customized marker color


ax = [Link](x='Year', y='Revenue', kind='line', title='Revenue Over Years',
marker='o', markerfacecolor='red', markeredgecolor='blue', linewidth=2, mar

# Customize labels
[Link]('Year')
[Link]('Amount (in $)')
display([Link]())
In [15]:

<Figure size 640x480 with 0 Axes>


<Figure size 640x480 with 0 Axes>

In [ ]:
Refer below link for marker styles:
[Link]

2. Bar Plot:
A bar plot is used to represent categorical data with rectangular bars. It is commonly used to compare data
between different categories.

Example 1: Bar Plot using Matplotlib:

In [9]: import [Link] as plt

categories = ['Category A', 'Category B', 'Category C']


values = [25, 40, 30]

[Link](categories, values, color='g',alpha=0.5)


[Link]('Categories')
[Link]('Values')
[Link]('Bar Plot Example')
[Link]( )
[Link]()

[Link]() syntax:

[Link](x, height, width=0.8, align='center', color='', edgecolor='', label='', ...)

[Link]() parameters:

x: The x-coordinates of the left edges of the bars.


height: The heights of the bars, representing the values of the categories.
width: The width of the bars. (Optional, default is 0.8)
align: The alignment of the bars with respect to the x-coordinates. (Optional, default is 'center')
color: The color of the bars. (Optional)
edgecolor: The color of the edges of the bars. (Optional)
label: The label for the plot, used for creating a legend. (Optional)

[Link]() syntax:

[Link](b=True, which='major', axis='both', linestyle='-', linewidth=0.5, color='gray', alpha=0.5)

[Link]() parameters:

b: A boolean value indicating whether to show grid lines (True) or not (False). (Optional, default is True)
which: The grid lines to display. Possible values are 'major', 'minor', or 'both'. (Optional, default is
'major')
axis: The axis for which to display grid lines. Possible values are 'x', 'y', or 'both'. (Optional, default is
'both')
linestyle: The style of the grid lines. (Optional, default is '-')
linewidth: The width of the grid lines. (Optional, default is 0.5)
color: The color of the grid lines. (Optional, default is 'gray')
alpha: The transparency of the grid lines. (Optional, default is 0.5)

Example 2: Bar Plot with Customization:

In [16]: import [Link] as plt

categories = ['Category A', 'Category B', 'Category C']


values = [25, 40, 30]

[Link](categories, values, width=0.6, color='g', edgecolor='black', label='Data Points'


[Link]('Categories')
[Link]('Values')
[Link]('Customized Bar Plot')
[Link]()
[Link](linestyle='--', alpha=0.8)
[Link]()
In [3]: import [Link] as plt

# Example data
categories = ['Category A', 'Category B', 'Category C']
values = [10, 15, 5]

# Create a bar chart


[Link](categories, values, color='green', width=0.6, edgecolor='black', align='center')
[Link]('Categories')
[Link]('Values')
[Link]('Bar Chart Example')
[Link](axis='y')
[Link]()
3. Scatter Plot:
A scatter plot is used to visualize the relationship between two variables. Each data point is represented as a
dot on the plot.

Example: Scatter Plot using Matplotlib:

In [4]: import [Link] as plt

x = [1, 2, 3, 4, 5]
y = [10, 25, 18, 30, 22]

[Link](x, y, marker='o', color='r', label='Data Points')


[Link]('X-axis')
[Link]('Y-axis')
[Link]('Scatter Plot Example')
[Link]()
[Link](True)
[Link]()
In [2]: import [Link] as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 6, 8]

# Create a scatter plot


[Link](x, y, label='Scatter Plot', color='red', marker='o', s=100, edgecolor='black
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Scatter Plot Example')
[Link]()
[Link](True)
[Link]()
4. Subplots:
Subplots allow us to create multiple plots within a single figure. They are useful for comparing different
visualizations side by side.

Example: Subplots using Matplotlib:

In [12]: import [Link] as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 25, 18, 30, 22]
y2 = [5, 12, 15, 8, 10]

# Creating a 2x2 subplot grid


# 1st Subplot
[Link](2, 2, 1)
[Link](x, y1, marker='o', linestyle='-', color='b', label='Data Points')
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Line Plot')
[Link](linestyle='--', alpha=0.8)

# 2nd Subplot
[Link](2, 2, 2)
[Link](x, y2, color='g', alpha=0.8)
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Bar Plot')
[Link](linestyle='--', alpha=0.8)

# 3rd Subplot
[Link](2, 2, 3)
[Link](x, y2, color='r', alpha=0.3)
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Bar Plot 2')
[Link](linestyle='--', alpha=0.8)

# 4th Subplot
[Link](2, 2, 4)
[Link](y1, y2, color='k', alpha=0.8)
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Scatter Plot')
[Link](linestyle='--', alpha=0.8)

plt.tight_layout() # Adjusts the spacing between subplots

[Link]()

Plotting Histogram
In [4]: import [Link] as plt

# Example data
data = [2, 3, 3, 4, 5, 5, 6, 6, 6, 7, 8, 8, 8, 8, 9]

# Create a histogram
[Link](data, bins=5, color='purple', edgecolor='black')
[Link]('Values')
[Link]('Frequency')
[Link]('Histogram Example')
[Link](axis='y')
[Link]()
Box Plot
In [5]: import [Link] as plt

# Example data
data = [25, 30, 35, 40, 45, 50, 55, 60, 65, 70]

# Create a box plot


[Link](data, vert=False, patch_artist=True, boxprops=dict(facecolor='yellow'))
[Link]('Values')
[Link]('Box Plot Example')
[Link](axis='x')
[Link]()
Pie Chart
In [6]: import [Link] as plt

# Example data
labels = ['Label 1', 'Label 2', 'Label 3']
sizes = [30, 50, 20]

# Create a pie chart


[Link](sizes, labels=labels, autopct='%1.1f%%', colors=['red', 'green', 'blue'])
[Link]('Pie Chart Example')
[Link]()
In [ ]:

In [ ]:

In [ ]:

5. Showing Images on Plots:


Matplotlib allows us to display images directly on plots, which is useful for visualizing data with images.

Example: Showing Image on Plot using Matplotlib:

In [6]: import [Link] as plt


import [Link] as mpimg

# Load the image


img = [Link](r"C:\Users\ramak\Desktop\img_disp.jpeg")

# Display the image on a plot


[Link](img)
[Link]('off') # Turn off axis ticks and labels
[Link]()
Histogram of image
In [ ]:

6. Plot Properties, Legends, Labels, Axis, and Other Features:


Matplotlib provides numerous features for customizing plots, including plot properties, legends, labels, axis
formatting, and more.

Example: Customizing Plot Properties and Legends:

In [17]: import [Link] as plt

x = [1, 2, 3, 4, 5]
y = [10, 25, 18, 30, 22]

[Link](x, y, marker='o', linestyle='-', color='b', label='Data Points')


[Link]('X-axis')
[Link]('Y-axis')
[Link]('Customized Line Plot')
[Link](loc='upper right')
[Link](True, linestyle='--', alpha=0.8)
[Link](range(1, 6), ['One', 'Two', 'Three', 'Four', 'Five'])
[Link](range(0, 35, 5))
[Link](1, 5.5)
[Link](0, 35)
[Link]()
Summary:
Matplotlib is a powerful library for creating various types of plots and visualizations in Python.

In this lesson, we explored line plots, bar plots, scatter plots, subplots, displaying images on plots,
customizing plot properties, legends, labels, and axis formatting.

By utilizing the flexibility and capabilities of Matplotlib, you can create professional and insightful
visualizations for your data analysis and presentation needs.

You might also like