1⃣️,开始使用Matplotlib作图

1. A brief introduction to NumPy arrays

Creating an array (an object of type ndarray) is simple:

In [1]: import numpy as np
In [2]: x = np.array([1, 2, 3])
In [3]: x
Out[3]: array([1, 2, 3])

We can pass a list or a tuple to array() and in return, we have an array object.

In [14]: range(6)
Out[14]: [0, 1, 2, 3, 4, 5]
In [15]: np.arange(6)
Out[15]: array([0, 1, 2, 3, 4, 5])

range(start, end, step),返回一个list对象,起始值为start,终止值为end,但不含终止值,步长为step。只能创建int型list。

arange(start, end, step),与range()类似,但是返回一个array对象。需要引入import numpy as np,并且arange可以使用float型数据。

2. Adding a grid---grid()

x = np.arange(1, 5)
plt.plot(x, x*1.5, x, x*3.0, x, x/3.0)
plt.grid(True)
plt.show()

3. Handing axes

Matplotlib automatically sets the limits of the figure to precisely contain the plotted datasets. However, sometimes we want to set the axes limits ourselves (defining the scale of the chart).

plt.axis()-----axis() without parameters, it returns the actual axis limits;

plt.axis(xmin=NNN, xmax=NNN, ymin=NNN, ymax=NNN)

plt.xlim() ----control the limits for each axis separately

plt.ylim()

4. Adding labels

Another important piece of information to add to a plot is the axes labels, since they usually specify what kind of data we are plotting.

plt.xlabel('title in x-axis')

plt.ylabel('title in y-axis')

5. Adding a title and legend

plt.title('Plot title')

Legends are used to explain what each line means in the current figure.

plt.legend()

6. A complete example

import matplotlib.pyplot as plt

import numpy as np

import matplotlib.pyplot as plt

x = np.arange(1, 5)

plt.plot(x, x*1.5, label='Normal')

plt.plot(x, x*3.0, label='Fast')

plt.plot(x, x/3.0, label='Slow')

plt.grid(True)

plt.title('Sample Growth of a Measure')

plt.xlabel('Samples')

plt.ylabel('Values Measured')

plt.legend(loc='upper left')

plt.show()

7. Saving plots to a file---savefig()

We can set the DPI value when saving by passing the additional keyword argument dpi to savefig(). This is explained with the help of the following line of code:

plt.savefig('plot123_2.png', dpi=200)

8. Configuring through the Python code

Matplotlib provides a way to change the settings for the current session, be it a script or program or an interactive session with the Python interpreter or IPython. matplotlib.rcParams is a handy dictionary, global to the whole matplotlib module, which contains default configuration settings (overridden by matplotlibrc files, if present).

mpl.rcParams[''] =



2018年10月20日 大阪で

你可能感兴趣的:(1⃣️,开始使用Matplotlib作图)