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

Notebook 1 - Matplotlib Basics

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)
0 views15 pages

Notebook 1 - Matplotlib Basics

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
You are on page 1/ 15

11/14/2019 Notebook 1 - Matplotlib Basics

Notebook 1 - MatplotLib Basics


In [1]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns #using for builtin datasets

In [8]:

plt.style.use('seaborn-white')
print(plt.style)

<module 'matplotlib.style' 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]

plt.figure(figsize=(9, 3))

plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.show()

First Simple Plot

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 1/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [9]:

plt.plot([1, 9, 3, 4])
plt.show()

In [10]:

plt.plot([1, 9, 3, 4], [10, 20, 30, 40])


plt.show()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 2/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [4]:

x = range(1,6)
plt.plot(x, [xi**2 for xi in x])
plt.show()

In [5]:

x = np.arange(1.0, 6.0, 0.1)


plt.plot(x, [xi**3 for xi in x])
plt.show()

Multiline Plot

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 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]

plt.plot(x, y)
plt.plot(x, z)
plt.show()

Grids, Axes, Titles and Labels

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 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]

plt.plot(x,y)
plt.grid(False)
plt.axis(xmin = 0, xmax = 9, ymin = 0, ymax = 7)
plt.show()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 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]

plt.plot(x,y)
plt.xlabel("Age")
plt.ylabel("Sales amount")
plt.title("Sales amount by Age")
plt.show()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 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]

plt.title("Legend Demo")

plt.plot(x,y, label="regular")
plt.plot(y,x, label="reverse")

plt.legend()

plt.show()

Complete Example

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 7/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [12]:

x = np.arange(1, 5)

plt.title("Matplotlib Graphs")

plt.plot(x, x*1, label = "regular")


plt.plot(x, x*2, label = "squared")

plt.grid(False)
plt.xlabel('numbers')
plt.ylabel('derived')

plt.legend()

plt.show()

Save to File

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 8/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [13]:

x = np.arange(1, 5)

plt.title("Matplotlib Graphs - Save to File")

plt.plot(x, x*1, label = "regular")


plt.plot(x, x*2, label = "squared")

plt.grid(False)
plt.xlabel('numbers')
plt.ylabel('derived')

plt.legend()

plt.savefig("complete_plot.png")

plt.show()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 9/15


11/14/2019 Notebook 1 - Matplotlib Basics

Decoration with Plot Styles and Types


In [14]:

x = np.arange(1, 5)

plt.title("Matplotlib Graphs Colors")

plt.plot(x, 'y')
plt.plot(x + 1, 'g')
plt.plot(x + 2, 'r')

plt.grid(False)
plt.xlabel('numbers')
plt.ylabel('derived')

plt.show()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 10/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [15]:

x = np.arange(1, 5)

plt.title("Matplotlib Graphs - Line Style")

plt.plot(x, '-')
plt.plot(x + 1, '*')
plt.plot(x + 2, '--')

plt.grid(False)
plt.xlabel('numbers')
plt.ylabel('derived')

plt.show()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 11/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [16]:

x = np.arange(1, 5)

plt.title("Matplotlib Graphs - Marker Style")

plt.plot(x, 'D')
plt.plot(x + 1, 'o')
plt.plot(x + 2, 's')

plt.grid(False)
plt.xlabel('numbers')
plt.ylabel('derived')

plt.show()

In [ ]:

x = np.arange(1, 5)

plt.title("Matplotlib Graphs - Customizations")

plt.plot(x, 'cD-')
plt.plot(x + 1, 'go')
plt.plot(x + 2, 'rs--')

plt.grid(False)
plt.xlabel('numbers')
plt.ylabel('derived')

plt.show()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 12/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [17]:

x = np.arange(1, 5)

plt.title("Matplotlib Graphs - Customizations")

plt.plot(x, color="red", linestyle="dashdot", linewidth=3, marker="D", markersize=10)

plt.grid(False)
plt.xlabel('numbers')
plt.ylabel('derived')

plt.show()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 13/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [18]:

x = np.arange(1, 5)

plt.title("Matplotlib Graphs - Ticks")

plt.plot(x, color="red", linestyle="dashdot", linewidth=3, marker="D", markersize=10)


plt.xticks(range(len(x)), ['Pune', 'Mumbai', "Bangalore", "Delhi"])
plt.yticks(range(0, 8, 2))
plt.grid(False)
plt.xlabel('numbers')
plt.ylabel('derived')

plt.show()

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 14/15


11/14/2019 Notebook 1 - Matplotlib Basics

In [ ]:

localhost:8888/notebooks/MachineLearning/Exam-DV/Notebook 1 - Matplotlib Basics.ipynb 15/15

You might also like