码迷,mamicode.com
首页 > 其他好文 > 详细

Getting Started with Matplotlib

时间:2019-12-31 01:44:13      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:app   asi   top   Opens   follow   des   bec   sts   ott   

In this part, we will:

  • Explore the basic ploting capabilities of Matplotlib for single or multiple lines.
  • Add informations to to the plots such as legends, axis labels and titles.
  • Save a plot to a file.
  • Describle the interaction with Ipython.
  • Customize[自定义] Matplotlib, both through configuration files and Python code.

Let‘s start looking at graph.

First plots with Matplotlib

Here is our first examples.

import matplotlib.pyplot as plt  # import the main Matplotlib submodule for ploting, pyplot
plt.plot([1, 4, 2, 6, 8, 5])  # This code line is the actual plotting command
plt.show()  # This command actually opens the window containing the plot image.

This code gives the output shown in the following screenshot:

  技术图片

It‘s strongly encouraged to never import like:

from <module> import *

Of course, we can also explicitly specify both the lists:

import matplotlib.pyplot as plt
x = range(6)
plt.plot(x, [xi ** 2 for xi in x])
plt.show()

That result in the following screenshot:

技术图片

So we can use arrange( to generate a finer range:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.0, 6.0, 0.01)
plt.plot(x, [x**2 for x in x])
plt.show()

技术图片

Multiline plots

It‘s fun to plot a line, but it‘s even more fun when we can plot more than one line on the same figure. This is really easy with Matplotlib as we can simply plot all the lines that we want before calling show(). Have a look at the following code and screenshot:

import matplotlib.pyplot as plt
x = range(1, 5)
plt.plot(x, [xi*1.5 for xi in x])
plt.plot(x, [x**3.0 for x in x])
plt.plot(x, [xi/3.0 for xi in x])
plt.show()

技术图片

Note how Matplotlib automatically chooses different colors for each line—green for the first line, blue for the second line, and red for the third one (from top to bottom).

Can you tell why a float value was used in line [5]? Try it yourself with an integer one and you‘ll see. The answer is that if divided by 3 (that is, by using an integer coefficient), the result will also be an integer. So you‘ll see a line plot like "stairs".

plot() supports another syntax useful in this situation. We can plot multiline figures by passing the X and Y values list in a single plot() call:

import matplotlib.pyplot as plt
x = range(1, 5)
plt.plot(x, [xi*1.5 for xi in x], x, [x**3.0 for x in x], x, [xi/3.0 for xi in x])
plt.show()

The preceding code simply rewrites the previous example with a different syntax.

While list comprehensions are a very powerful tool, they can generate a little bit of confusion in these simple examples. So we‘d like to show another feature of the arange() function of NumPy:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1, 5)
plt.plot(x, x*1.5, x**3.0, x/3.0)
plt.show()

技术图片

Here, we take advantage of the NumPy array objects returned by arange().

The multiline plot is possible because, by default, the hold property is enabled (consider it as a declaration to preserve all the plotted lines on the current figure instead of replacing them at every plot() call). Try this simple example and see what happens:

Getting Started with Matplotlib

标签:app   asi   top   Opens   follow   des   bec   sts   ott   

原文地址:https://www.cnblogs.com/Junlong/p/12122272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!