0% found this document useful (0 votes)
1 views25 pages

Matplotlib

Matplotlib is a powerful Python library for creating a wide range of visualizations, including line plots, scatter plots, bar charts, histograms, and 3D plots. It offers extensive customization options, integration with NumPy and Pandas, and support for animations and statistical plots. The library is well-documented and supported by a large community, making it suitable for both simple and complex data visualization tasks.

Uploaded by

singhqwerty211
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)
1 views25 pages

Matplotlib

Matplotlib is a powerful Python library for creating a wide range of visualizations, including line plots, scatter plots, bar charts, histograms, and 3D plots. It offers extensive customization options, integration with NumPy and Pandas, and support for animations and statistical plots. The library is well-documented and supported by a large community, making it suitable for both simple and complex data visualization tasks.

Uploaded by

singhqwerty211
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

Using Matplotlib

Introduction
Matplotlib is a versatile and widely used plotting library in Python that
provides a variety of features for creating static, animated, and interactive
visualizations. Here's a summary of various features of Matplotlib:
1. Plots and Charts:
o Line Plot: Basic and versatile for displaying data trends.
o Scatter Plot: Display individual data points and their relationships.
o Bar Chart: Represent categorical data with rectangular bars.
o Histogram: Visualize the distribution of continuous data.
o Pie Chart: Display data as slices of a pie to show proportions.
2. Customization:
o Labels and Titles: Customize axis labels, plot titles, and legends.
o Colors and Styles: Choose colors, line styles, and markers to enhance visual appearance.
o Annotations: Add text annotations, arrows, or shapes to highlight specific points.
Introduction
3. Subplots:
o Multiple Plots: Create multiple plots within a single figure using subplots.
o Grid Layouts: Arrange subplots in a grid for better organization.
4. 3D Plotting:
o Surface Plots: Visualize 3D data with surface plots.
o Scatter 3D: Create 3D scatter plots for three-dimensional data.
5. Animations:
o Animated Plots: Generate dynamic visualizations by updating data over time.
o Interactive Widgets: Use widgets to control and interact with animated plots.
6. Image and Colormap:
o Image Display: Show and manipulate images using imshow.
o Colormaps: Apply different color maps to enhance visualization.
Introduction
7. Statistical Plots:
o Box Plots: Display statistical summary of data distribution.
o Violin Plots: Combine box plots with kernel density estimation.
o Heatmaps: Visualize matrix-like data with a color-coded representation.
8. Backend Support:
o Interactive Backends: Choose interactive backends for dynamic and interactive plots.
o Exporting: Save plots in various formats (PNG, PDF, SVG) for external use.
9. Integration with NumPy and Pandas:
o NumPy Integration: Direct support for NumPy arrays.
o Pandas Integration: Seamlessly work with Pandas Data Frames for data visualization.
10. Latex Rendering:
o Mathematical Text: Render mathematical equations using LaTeX syntax.
Introduction
11. Seaborn Integration:
o Statistical Visualization: Matplotlib can be combined with Seaborn for enhanced
statistical plotting capabilities.
12. Community and Documentation:
o Large Community: Active community support and contributions.
o Documentation: Extensive and well-maintained documentation for guidance.
Matplotlib's flexibility and extensive functionality make it suitable for a
wide range of visualization tasks, from simple plots to complex,
publication-quality graphics. Its integration with other scientific libraries
like NumPy and Pandas further enhances its utility in data analysis and
visualization workflows.
Line Plot
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
[Link](x, y)
[Link]('Line Plot')
[Link]('X-axis')
[Link]('Y-axis')
[Link]()
Plotting x and y points
• The plot() function is used to draw points (markers) in a diagram. By default, the plot() function draws a line
from point to point.

• The function takes parameters for specifying points in the diagram. Parameter 1 is an array containing the
points on the x-axis. Parameter 2 is an array containing the points on the y-axis.

• If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the plot
function.

• Example: Draw a line in a diagram from position (1, 3) to position (8, 10):

• import [Link] as plt


import numpy as np
xpoints = [Link]([1, 8])
ypoints = [Link]([3, 10])
[Link](xpoints, ypoints)
[Link]()
Plotting Without Line
• To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings’.
• Example: Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):
• import [Link] as plt
import numpy as np
xpoints = [Link]([1, 8])
ypoints = [Link]([3, 10])
[Link](xpoints, ypoints, 'o')
[Link]()
Multiple Points
• We can plot as many points as you like, just make sure you have the same number of points in both axis.
• Example: Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10):
• import [Link] as plt
import numpy as np
xpoints = [Link]([1, 2, 6, 8])
ypoints = [Link]([3, 8, 1, 10])
[Link](xpoints, ypoints)
[Link]()
Default X-Points
• If we do not specify the points on the x-axis, they will get the default values 0, 1, 2, 3 etc., depending on the
length of the y-points. So, if we take the same example as above, and leave out the x-points, the diagram will
look like this:
• Example: Plotting without x-points:

import [Link] as plt


import numpy as np
ypoints = [Link]([3, 8, 1, 10, 5, 7])
[Link](ypoints)
[Link]()
line plot with multiple lines
import [Link] as plt
import numpy as np
x = [Link](0, 10, 100)
y1 = [Link](x)
y2 = [Link](x)
[Link](x, y1, label='sin(x)')
[Link](x, y2, label='cos(x)')
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Multiple Lines')
[Link]()
[Link]()
Scatter Plot
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
[Link](x, y)
[Link]('Scatter Plot')
[Link]('X-axis')
[Link]('Y-axis')
[Link]()
Scatter Plot with Colors
import [Link] as plt
import numpy as np
x = [Link](50)
y = [Link](50)
colors = [Link](50)
[Link](x, y, c=colors)
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Scatter Plot with Colors')
[Link]()
[Link]()
Bar Chart
import [Link] as plt
categories = ['A', 'B', 'C', 'D']
values = [3, 7, 1, 10]
[Link](categories, values)
[Link]('Bar Chart')
[Link]('Categories')
[Link]('Values')
[Link]()
Bar chart with error bars
import [Link] as plt
import numpy as np
categories = ['A', 'B', 'C', 'D']
values = [3, 7, 2, 5]
errors = [0.5, 1, 0.2, 0.8]
[Link](categories, values, yerr=errors, color='skyblue')
[Link]('Categories')
[Link]('Values')
[Link]('Bar Chart with Error Bars')
[Link]()
Histogram
import [Link] as plt
import numpy as np
data = [Link](1000)
[Link](data, bins=30,color='orange’,edgecolor='black')
[Link]('Histogram')
[Link]('Values')
[Link]('Frequency')
[Link]()
Pie Chart
import [Link] as plt
labels = ['Category A', 'Category B', 'Category C',
'Category D’]
sizes = [25, 30, 20, 25]
[Link](sizes, labels=labels, autopct='%1.1f%%',
startangle=90, colors=['lightcoral', 'lightblue',
'lightgreen', 'lightskyblue’])
[Link]('equal')
[Link]('Pie Chart')
[Link]()
Stacked Bar Chart
import [Link] as plt
categories = ['A', 'B', 'C', 'D']
values1 = [3, 7, 1, 10]
values2 = [5, 2, 8, 6]
[Link](categories, values1, label='Group 1')
[Link](categories, values2, label='Group 2', bottom=values1)
[Link]('Stacked Bar Chart')
[Link]('Categories')
[Link]('Values')
[Link]()
[Link]()
Stacked Area Plot
import [Link] as plt
import numpy as np
x = [Link](0, 10, 100)
y1 = [Link](x)
y2 = [Link](x)
[Link](x, y1,
y2,labels=['sin(x)','cos(x)'],colors=['lightcoral',
'lightblue'])
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Stacked Area Plot')
[Link]()
[Link]()
Box Plot
import [Link] as plt
import numpy as np
data = [Link](100)
[Link](data)
[Link]('Box Plot')
[Link]()
3D Plot
import [Link] as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = [Link]()
ax = fig.add_subplot(111, projection='3d')
x = [Link](10)
y = [Link](10)
z = [Link](10)
[Link](x, y, z)
[Link]('3D Scatter Plot')
[Link]()
3D surface plot
import [Link] as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x = [Link](-5, 5, 50)
y = [Link](-5, 5, 50)
X, Y = [Link](x, y)
Z = [Link]([Link](X**2 + Y**2))
fig = [Link]()
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')
[Link]()
Contour Plot
import [Link] as plt
import numpy as np
x = [Link](-5, 5, 100)
y = [Link](-5, 5, 100)
X, Y = [Link](x, y)
Z = [Link]([Link](X**2 + Y**2))
[Link](X, Y, Z)
[Link]('Contour Plot')
[Link]('X-axis')
[Link]('Y-axis')
[Link]()
Violin Plot
import [Link] as plt
import numpy as np
data = [[Link](0, std, 100) for std
in range(1, 4)]
[Link](data, showmeans=True,
showmedians=True)
[Link]([Link](1, 4), ['Group 1', 'Group
2', 'Group 3'])
[Link]('Groups')
[Link]('Values')
[Link]('Violin Plot')
[Link]()
Quiver Plot
In Python, the quiver plot is commonly used to visualize vector fields. A vector field is a mathematical construct
representing a collection of vectors in a particular space.
quiver([X, Y], U, V, [C], )
X, Y define the arrow locations, U, V define the arrow directions, and C optionally sets the color.

import [Link] as plt


import numpy as np
x = [Link](-2, 2, 10)
y = [Link](-2, 2, 10)
X, Y = [Link](x, y)
U=X
V=Y
[Link](X, Y, U, V, scale=20)
[Link]('Quiver Plot')
[Link]('X-axis')
[Link]('Y-axis')
[Link]()

You might also like