官术网_书友最值得收藏!

  • Machine Learning for OpenCV
  • Michael Beyeler
  • 142字
  • 2021-07-02 19:47:19

Producing a simple plot

Without further ado, let's create our first plot.

Let's say that we want to produce a simple line plot of the sine function, sin(x). We want the function to be evaluated at all points on the x-axis where 0 x < 10. We will use NumPy's linspace function to create a linear spacing on the x axis, from x values 0 to 10, and a total of 100 sampling points:

In [3]: import numpy as np
In [4]: x = np.linspace(0, 10, 100)

We can evaluate the sin function at all points x using NumPy's sin function and visualize the result by calling plt's plot function:

In [5]: plt.plot(x, np.sin(x))

Did you try it yourself? What happened? Did anything show up?

The thing is, depending on where you are running this script, you might not see anything. There are the following possibilities to consider:

  • Plotting from a .py script: If you are running Matplotlib from a script, you need to simply call as follows:
         plt.show()

Call this at the end and your plot will show up!

  • Plotting from an IPython shell: This is actually one of the most convenient ways to run Matplotlib interactively. To make the plots show up, you need to use what is called the %matplotlib magic command after starting IPython:
          In [1]: %matplotlib
Using matplotlib backend: TkAgg
In [2]: import matplotlib.pyplot as plt

Then, all your plots will show up automatically without having to call plt.show() every time.

  • Plotting from a Jupyter Notebook: If you are viewing this code from a browser-based Jupyter Notebook, you need to use the same %matplotlib magic. However, you also have the option of embedding graphics directly in the notebook, with two possible outcomes:
    • %matplotlib notebook will lead to interactive plots embedded within the notebook
    • %matplotlib inline will lead to static images of your plots embedded within the notebook

In this book, we will generally opt for the inline option:

In [6]: %matplotlib inline

Now let's try this again:

In [7]: plt.plot(x, np.sin(x))
Out[7]: [<matplotlib.lines.Line2D at 0x7f3aac426eb8>]

The preceding command gives the following output:

Plotting the sin function using Matplotlib

If you want to save the figure for later, you have the option to do so directly from within IPython or Jupyter Notebook:

In [8]: savefig('figures/02.04-sine.png')

Just make sure that you use a supported file ending, such as .jpg, .png, .tif, .svg, .eps, or .pdf.

You can change the style of your plots by running plt.style.use(style_name) after importing Matplotlib. All available styles are listed in plt.style.available. For example, try plt.style.use('fivethirtyeight'), plt.style.use('ggplot'), or plt.style.use('seaborn-dark'). For added fun, run plt.xkcd(), then try and plot something else.
主站蜘蛛池模板: 上杭县| 邛崃市| 永吉县| 宜城市| 高唐县| 仪征市| 新安县| 文登市| 陈巴尔虎旗| 望奎县| 建水县| 长垣县| 循化| 博野县| 十堰市| 外汇| 厦门市| 庄浪县| 竹山县| 平利县| 扬中市| 金门县| 司法| 武夷山市| 兴山县| 榆社县| 上犹县| 双桥区| 青海省| 太原市| 青铜峡市| 股票| 格尔木市| 保亭| 中卫市| 遵义县| 瑞金市| 新密市| 木兰县| 虎林市| 元氏县|