0% found this document useful (0 votes)
18 views15 pages

Matplotlib Basics for Beginners

This document is a Jupyter notebook focused on the basics of Matplotlib for data visualization in Python. It includes various examples of plotting techniques such as bar plots, scatter plots, line plots, and customization options like titles, labels, and styles. The notebook demonstrates how to create and save plots, as well as how to customize their appearance with colors, line styles, and markers.
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)
18 views15 pages

Matplotlib Basics for Beginners

This document is a Jupyter notebook focused on the basics of Matplotlib for data visualization in Python. It includes various examples of plotting techniques such as bar plots, scatter plots, line plots, and customization options like titles, labels, and styles. The notebook demonstrates how to create and save plots, as well as how to customize their appearance with colors, line styles, and markers.
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

11/14/2019 Notebook 1 - Matplotlib Basics

Notebook 1 - MatplotLib Basics


In [1]:

import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns #using for builtin datasets

In [8]:

[Link]('seaborn-white')
print([Link])

<module '[Link]' from 'C:\\Users\\PYPAWAR\\Anaconda3\\lib\\site-pa


ckages\\matplotlib\\style\\__init__.py'>

In [2]:

names = ['group_a', 'group_b', 'group_c']


values = [1, 10, 100]

[Link](figsize=(9, 3))

[Link](131)
[Link](names, values)
[Link](132)
[Link](names, values)
[Link](133)
[Link](names, values)
[Link]('Categorical Plotting')
[Link]()

First Simple Plot

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 1/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [9]:

[Link]([1, 9, 3, 4])
[Link]()

In [10]:

[Link]([1, 9, 3, 4], [10, 20, 30, 40])


[Link]()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 2/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [4]:

x = range(1,6)
[Link](x, [xi**2 for xi in x])
[Link]()

In [5]:

x = [Link](1.0, 6.0, 0.1)


[Link](x, [xi**3 for xi in x])
[Link]()

Multiline Plot

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 3/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [6]:

x = [0, 1, 2, 3, 4, 5]
y = [0, 5, 3, 2, 1, 2]
z = [4, 9, 3, 2, 8, 1]

[Link](x, y)
[Link](x, z)
[Link]()

Grids, Axes, Titles and Labels

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 4/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [7]:

x = [0, 1, 2, 3, 4, 5]
y = [0, 5, 3, 2, 1, 2]

[Link](x,y)
[Link](False)
[Link](xmin = 0, xmax = 9, ymin = 0, ymax = 7)
[Link]()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 5/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [8]:

x = [0, 1, 2, 3, 4, 5]
y = [0, 5, 3, 2, 1, 2]

[Link](x,y)
[Link]("Age")
[Link]("Sales amount")
[Link]("Sales amount by Age")
[Link]()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 6/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [11]:

x = [0, 1, 2, 3, 4, 5]
y = [0, 5, 3, 2, 1, 2]

[Link]("Legend Demo")

[Link](x,y, label="regular")
[Link](y,x, label="reverse")

[Link]()

[Link]()

Complete Example

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 7/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [12]:

x = [Link](1, 5)

[Link]("Matplotlib Graphs")

[Link](x, x*1, label = "regular")


[Link](x, x*2, label = "squared")

[Link](False)
[Link]('numbers')
[Link]('derived')

[Link]()

[Link]()

Save to File

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 8/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [13]:

x = [Link](1, 5)

[Link]("Matplotlib Graphs - Save to File")

[Link](x, x*1, label = "regular")


[Link](x, x*2, label = "squared")

[Link](False)
[Link]('numbers')
[Link]('derived')

[Link]()

[Link]("complete_plot.png")

[Link]()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 9/15


11/14/2019 Notebook 1 - Matplotlib Basics

Decoration with Plot Styles and Types


In [14]:

x = [Link](1, 5)

[Link]("Matplotlib Graphs Colors")

[Link](x, 'y')
[Link](x + 1, 'g')
[Link](x + 2, 'r')

[Link](False)
[Link]('numbers')
[Link]('derived')

[Link]()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 10/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [15]:

x = [Link](1, 5)

[Link]("Matplotlib Graphs - Line Style")

[Link](x, '-')
[Link](x + 1, '*')
[Link](x + 2, '--')

[Link](False)
[Link]('numbers')
[Link]('derived')

[Link]()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 11/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [16]:

x = [Link](1, 5)

[Link]("Matplotlib Graphs - Marker Style")

[Link](x, 'D')
[Link](x + 1, 'o')
[Link](x + 2, 's')

[Link](False)
[Link]('numbers')
[Link]('derived')

[Link]()

In [ ]:

x = [Link](1, 5)

[Link]("Matplotlib Graphs - Customizations")

[Link](x, 'cD-')
[Link](x + 1, 'go')
[Link](x + 2, 'rs--')

[Link](False)
[Link]('numbers')
[Link]('derived')

[Link]()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 12/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [17]:

x = [Link](1, 5)

[Link]("Matplotlib Graphs - Customizations")

[Link](x, color="red", linestyle="dashdot", linewidth=3, marker="D", markersize=10)

[Link](False)
[Link]('numbers')
[Link]('derived')

[Link]()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 13/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [18]:

x = [Link](1, 5)

[Link]("Matplotlib Graphs - Ticks")

[Link](x, color="red", linestyle="dashdot", linewidth=3, marker="D", markersize=10)


[Link](range(len(x)), ['Pune', 'Mumbai', "Bangalore", "Delhi"])
[Link](range(0, 8, 2))
[Link](False)
[Link]('numbers')
[Link]('derived')

[Link]()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 14/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [ ]:

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib [Link] 15/15

Common questions

Powered by AI

Different plot styles in Matplotlib, such as line styles '-', '*', '--', and marker styles 'D', 'o', 's', allow customization of plot appearance to enhance data visualization. Using colors, such as 'red', 'green', and 'blue', and adjusting line width and marker sizes can improve clarity and make the graph more tailored to the audience's needs .

In Matplotlib, specific data points can be emphasized by using markers (e.g., 'D', 'o'), changing colors, or increasing marker size. Additionally, annotations or changing the line style for segments can highlight data significance. These techniques draw attention to critical points or trends, aiding in audience focus and comprehension .

The 'plt.xticks' and 'plt.yticks' functions in Matplotlib are used to customize the tick labels on the x and y axes, respectively. They can transform number-based labels into more descriptive names, such as city names on the x-axis ('Pune', 'Mumbai') and control the spacing of ticks on the y-axis to improve readability and focus on specific data points .

Using numpy's array manipulation functions with Matplotlib provides efficiency and simplicity in creating plots. Numpy allows for quick generation and transformation of data arrays, which can be directly plotted, making it possible to handle large datasets effectively and perform mathematical operations directly on arrays .

Grid visibility in Matplotlib affects plot interpretation by adding reference lines for better comparison and alignment. However, turning grids off can focus the viewer's attention directly on the data itself, avoiding potential distractions from grid lines. The choice depends on the plot's purpose and complexity .

Saving plots to files is crucial in data analysis for documentation, sharing, and reporting results. In Matplotlib, you save a plot by calling plt.savefig('filename.png') after creating your plot with plt.plot() and plt.show(). This allows the plot to be reused and referenced outside of the interactive environment .

Matplotlib allows displaying multiple plots in a single figure using subplots. The plt.subplot() function can be used to specify the layout, such as 131 to create three side-by-side plots in one row. This method helps in comparing different datasets or visualizing multiple aspects of data simultaneously .

Using titles, labels, and legends in Matplotlib plots provides clarity and context to the visualization. Titles inform the viewer about the graph's overall purpose. Labels on axes help interpret the data by indicating what variables are being compared. Legends distinguish between different datasets in the plot, making complex plots easier to understand .

Line styles (e.g., '-', '--') and marker styles (e.g., 'D', 'o') complement each other in Matplotlib plots by enhancing the visual distinction between different datasets. While line styles affect the continuity and form of the line, marker styles highlight individual data points, improving data interpretation and presentation .

Setting axis limits manually in Matplotlib allows precise control over the area of data being visualized. This can highlight specific data ranges, control outliers, and improve focus on significant trends without automatic scaling potentially skewing perception. However, it requires careful consideration to maintain data integrity .

You might also like