将Matplotlib与Jupyter Notebook一起使用

2021年3月15日08:36:06 发表评论 692 次浏览

Jupyter笔记本

是一个开放源代码的Web应用程序, 允许你创建和共享包含实时代码, 方程式, 可视化效果和叙述文本的文档。用途包括:数据清理和转换, 数值模拟, 统计模型, 数据可视化, 机器学习等。

注意:有关更多信息, 请参阅如何使用Jupyter Notebook-终极指南

Matplotlib是用于数据可视化的最受欢迎的Python软件包之一。它是一个跨平台的库, 用于从数组中的数据制作2D图。开始时, 你只需进行必要的导入, 准备一些数据, 然后就可以借助情节()功能。完成后, 请记住使用节目()功能。

Matplotlib用Python编写并利用Numpy, 是Python的数字数学扩展。它由多个图组成, 例如:–

  • 线
  • 酒吧
  • 分散
  • 直方图
  • 还有很多

安装

用以下命令安装Matplotlib

点子

Matplotlib也可以使用Python包管理器pip安装。要使用pip安装Matplotlib, 请打开终端窗口并输入:

pip install matplotlib

使用以下命令安装Matplotlib

Anaconda提示

Matplotlib可以与Anaconda Prompt一起安装。如果你的计算机上有Anaconda Prompt, 通常可以在Windows"开始"菜单中看到。要安装Matplotlib, 请打开Anaconda Prompt并输入:

conda install matplotlib
图形
图形

将Matplotlib与Jupyter Notebook一起使用

安装完成后。让我们开始将Matplotlib与Jupyter Notebook一起使用。我们将使用Matplotlib在Jupyter Notebook中绘制各种图形。

线图

# importing matplotlib module 
from matplotlib import pyplot as plt 
  
# x-axis values 
x = [ 5 , 2 , 9 , 4 , 7 ] 
  
# Y-axis values 
y = [ 10 , 5 , 8 , 4 , 2 ] 
  
# Function to plot 
plt.plot(x, y) 
  
# function to show the plot 
plt.show()

输出:

matplotlib-with-jupyter-1

条形图

# importing matplotlib module 
from matplotlib import pyplot as plt 
  
# x-axis values 
x = [ 5 , 2 , 9 , 4 , 7 ] 
  
# Y-axis values 
y = [ 10 , 5 , 8 , 4 , 2 ] 
  
# Function to plot 
plt.bar(x, y) 
  
# function to show the plot 
plt.show()

输出:

matplotlib-with-jupyter-2

直方图

# importing matplotlib module 
from matplotlib import pyplot as plt 
  
# Y-axis values 
y = [ 10 , 5 , 8 , 4 , 2 ] 
  
# Function to plot histogram 
plt.hist(y) 
  
# Function to show the plot 
plt.show()

输出:

matplotlib-with-jupyter-3

散点图

# importing matplotlib module 
from matplotlib import pyplot as plt 
  
# x-axis values 
x = [ 5 , 2 , 9 , 4 , 7 ] 
  
# Y-axis values 
y = [ 10 , 5 , 8 , 4 , 2 ] 
  
# Function to plot scatter 
plt.scatter(x, y) 
  
# function to show the plot 
plt.show()

输出:

matplotlib-with-jupyter-4

在图中添加标题并标记轴

我们可以使用以下命令为图添加标题

matplotlib.pyplot.title("My title")

我们可以使用以下功能标记x轴和y轴

matplotlib.pyplot.xlabel("Time (Hr)")

matplotlib.pyplot.ylabel("Position (Km)")

范例:

# importing matplotlib module 
from matplotlib import pyplot as plt 
  
# x-axis values 
x = [ 5 , 2 , 9 , 4 , 7 ] 
  
# Y-axis values 
y = [ 10 , 5 , 8 , 4 , 2 ] 
  
# Function to plot 
plt.scatter(x, y) 
  
# Adding Title
plt.title( "GeeksFoeGeeks" )
  
# Labeling the axes
plt.xlabel( "Time (hr)" )
plt.ylabel( "Position (Km)" )
  
# function to show the plot 
plt.show()

输出如下:

matplotlib-with-jupyter-5

我们也可以在同一单元中编写程序进行打印

多个图

一起。

我们可以通过重复执行程序中的show()函数, 将这些图形垂直地逐个打印。

subplot()

为了将它们水平打印。

from matplotlib import pyplot as plt 
  
  
x = [ 1 , 2 , 3 , 4 , 5 ] 
y = [ 1 , 4 , 9 , 16 , 25 ] 
plt.scatter(x, y) 
  
# function to show the plot 
plt.show()
  
plt.plot(x, y)
  
# function to show the plot 
plt.show()

输出如下

matplotlib-jupyter

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: