Matplotlib 简明教程
Matplotlib - Jupyter Notebook
Jupyter 是一个宽松的缩写,表示 Julia、Python 和 R。这些编程语言是 Jupyter 应用程序最初的目标语言,但如今该 Jupyter 技术还支持其他许多语言。
Jupyter is a loose acronym meaning Julia, Python, and R. These programming languages were the first target languages of the Jupyter application, but nowadays, the notebook technology also supports many other languages.
2001 年,费尔南多·佩雷斯开始开发 IPython。 IPython 是一个用于在多种编程语言中进行交互计算的命令外壳,最初是为 Python 开发的。
In 2001, Fernando Pérez started developing Ipython. IPython is a command shell for interactive computing in multiple programming languages, originally developed for the Python.
Jupyter Notebook 中 Matplotlib 提供一个交互式环境,让我们可以在代码中创建可视化效果。让我们开始采取步骤,在 Jupyter Notebook 中使用 Matplotlib。
Matplotlib in Jupyter Notebook provides an interactive environment for creating visualizations right alongside our code. Let’s go through the steps to start using Matplotlib in a Jupyter Notebook.
在 Jupyter Notebook 中,Matplotlib 库提供了一种便捷的方式来交互式可视化数据,它允许在处理数据分析、机器学习或任何其他基于 Python 的项目时进行探索性和解释性工作流程。
Matplotlib library in Jupyter Notebook provides a convenient way to visualize data interactively by allowing for an exploratory and explanatory workflow when working on data analysis, machine learning or any other Python-based project.
考虑 IPython 提供的以下功能−
Consider the following features provided by IPython −
-
Interactive shells (terminal and Qt-based).
-
A browser-based notebook with support for code, text, mathematical expressions, inline plots and other media.
-
Support for interactive data visualization and use of GUI toolkits.
-
Flexible, embeddable interpreters to load into one’s own projects.
2014 年,Fernando Pérez 宣布了 IPython 的一个衍生项目,名为 Project Jupyter。IPython 将继续作为 Python shell 和 Jupyter 的内核存在,而笔记本和其他与语言无关的 IPython 部分将移至 Jupyter 名称下。Jupyter 为 Julia、R、Haskell 和 Ruby 添加了支持。
In 2014, Fernando Pérez announced a spin-off project from IPython called Project Jupyter. IPython will continue to exist as a Python shell and a kernel for Jupyter, while the notebook and other language-agnostic parts of IPython will move under the Jupyter name. Jupyter added support for Julia, R, Haskell and Ruby.
Starting Jupyter Notebook
以下是要在 Jupyter Notebook 中按部就班进行操作的步骤:
The below are the steps to be done by one by one to work in the Jupyter Notebook.
Launch Jupyter Notebook
打开 Anaconda Navigator。
Open Anaconda Navigator.
从 Navigator 中启动 Jupyter Notebook 或在终端/Anaconda Prompt 类型中键入 jupyter notebook
,然后按 Enter。
Launch Jupyter Notebook from the Navigator or in the terminal/Anaconda Prompt type jupyter notebook and hit Enter.
Create or Open a Notebook
一旦 Jupyter Notebook 在我们的网络浏览器中打开,然后导航到我们想要工作的目录。
Once Jupyter Notebook opens in our web browser then navigate to the directory where we want to work.
点击“新建”后,选择一个 Python 笔记本,通常称为 .ipynb
笔记本。
After click on "New" and choose a Python notebook which is often referred to as an "Untitled" notebook.
Import Matplotlib
在一个 Jupyter Notebook 单元格中,使用代码行导入 Matplotlib 库。
In a Jupyter Notebook cell import Matplotlib library by using the lines of code.
import matplotlib.pyplot as plt
%matplotlib inline
%matplotlib inline
是一个魔术命令,它告诉 Jupyter Notebook 在笔记本中内联显示 Matplotlib 绘图。
%matplotlib inline is a magic command that tells Jupyter Notebook to display Matplotlib plots inline within the notebook.
Create Plots
我们现在可以使用 Matplotlib 函数来创建我们的绘图。例如,让我们使用 numpy 数据创建一个线形图。
We can now use Matplotlib functions to create our plots. For example let’s create a line plot by using the numpy data.
import numpy as np
import matplotlib.pyplot as plt
# Generating sample data
x = np.linspace(0, 20, 200)
y = np.sin(x)
# Plotting the data
plt.figure(figsize=(8, 4))
plt.plot(x, y, label='sin(x)')
plt.title('Sine Wave')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.legend()
plt.grid(True)
plt.show()
Interact with Plots
一旦生成绘图,它将直接显示在单元格下面的笔记本中。如果我们在导入阶段使用了 .ipynb
笔记本而不是 .py
笔记本,我们可以与绘图交互,即平移、缩放。
Once the plot is generated then it will be displayed directly in the notebook below the cell. We can interact with the plot i.e. panning, zooming can be done if we used %matplotlib notebook instead of %matplotlib inline at the import stage.
Multiple Plots
我们可以通过创建新单元格并运行更多 Matplotlib 命令来创建多个绘图。
We can create multiple plots by creating new cells and running more Matplotlib commands.
Closing Jupyter Notebook
一旦我们完成在笔记本中的工作,我们可以从 Jupyter Notebook 界面关闭它,或关闭 Jupyter Notebook 启动的终端/Anaconda Prompt。
Once we have finished working in the notebook we can shut it down from the Jupyter Notebook interface or close the terminal/Anaconda Prompt where Jupyter Notebook was launched.
Hide Matplotlib descriptions in Jupyter notebook
为了在调用 plot()
方法时隐藏实例的 matplotlib 描述信息,我们可以执行以下步骤
To hide matplotlib descriptions of an instance while calling plot() method, we can take the following steps
-
Open Ipython instance.
-
import numpy as np
-
from matplotlib, import pyplot as plt
-
Create points for x, i.e., np.linspace(1, 10, 1000)
-
Now, plot the line using plot() method.
-
To hide the instance, use plt.plot(x); i.e., (with semi-colon)
-
Or, use _ = plt.plot(x)