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['<param name>'] = <value>



2018年10月20日 大阪で

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,160评论 0 10
  • 每日复盘 Objective 你对今天学的记得什么? 今天对战演习の时候觉得看着小伙伴们激烈的对战,觉得自己要补充...
    她念阅读 1,638评论 0 0
  • 图解执行流程.png 流程预处理/* 指令:gcc -E hello.c -o hello.i 作用:处理文件...
    qianranow阅读 2,732评论 1 4
  • 01 邻居家王阿姨的女儿小兰闹离婚,带着孩子住回了娘家。 小兰老公一下班就是躺在沙发上玩手机,过着饭来张口衣来伸手...
    子兮9077阅读 7,683评论 11 49
  • 发出去的朋友圈有的时候就找不到了 路总是越走越远,各自奔天涯之后 互道珍重不必回头 如果找不到坚持下去的理由,那就...
    设计与故事阅读 2,435评论 0 0