Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

Gray-Ice

个人博客兼个人网站

1.基本绘图

使用matplotlib库绘制折线图,由于很简单,所以下面直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from matplotlib import pyplot


# 数据在x轴位置,是一个可迭代对象
x = range(2, 26, 2)
# 数据在y轴的位置
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]
# x和Y的长度必须相等,如果不相等会报错
print(len(x))
print(len(y))
# 绘图
pyplot.plot(x, y)
# 展示
pyplot.show()

效果图如下:

image-20200528092144373

image

2.绘图常用操作

上面的是基本的绘图方法,那么下面是一些常用的操作:

设置图片大小,设置x轴、y轴刻度,以及保存图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from matplotlib import pyplot


# 设置图片大小,元组中第一个参数是宽,第二个参数是高,dpi是清晰度
pyplot.figure(figsize=(20, 8), dpi=80)
# 数据在x轴位置,是一个可迭代对象
x = range(2, 26, 2)
# 数据在y轴的位置
y = [15, 13, 14, 5, 17, 20, 25, 26, 26, 24, 22, 18]
# x和Y的长度必须相等
print(len(x))
print(len(y))
# x轴的刻度
xtick = [i/2 for i in range(0, 49)]
pyplot.xticks(xtick[::3])
# y轴的刻度
pyplot.yticks(range(min(y), max(y) + 1))
# 绘图
pyplot.plot(x, y)
# 保存图片,必须先绘制再保存
pyplot.savefig('./pic.png')

那么下面是效果图:
image-20200528095202645

旋转字体和设置轴标签

使用rotation参数可以旋转字体。

如果想要设置标签,可以使用: xlabel和ylabel,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from matplotlib import pyplot
import random

font_style = "Kaiti"

# a=[random.randint(20, 35) for i in range(120)],a表示10点到12点的每一分钟的气温,如何绘制折线图观察每分钟气温的变化情况?
x = list(range(0, 120))
y = [random.randint(20, 35) for i in range(120)]
pyplot.figure(figsize=(20, 8), dpi=80)
x_ticks = ["{}点:{}分".format(i, k) for i in range(10, 12) for k in range(0, 60)]
# fontproperties指定字体,fontsize指定大小,rotation旋转
pyplot.xticks(x[:: 10], x_ticks[:: 10], fontproperties=font_style, fontsize=10, rotation=0)
# xlabel设置x轴标签
pyplot.xlabel('时间', fontproperties=font_style, rotation=90)
# ylabel设置y轴标签
pyplot.ylabel('温度', fontproperties=font_style, rotation=0)
pyplot.plot(x, y)
pyplot.savefig('./template.png')

下面是效果图:

image-20200605165626456

显示网格

只需加上:

1
pyplot.grid()

即可。

默认绘制的横线数量为y轴的刻度的个数,竖线为x轴的刻度的个数。

若是认为线条过于显眼,可以加上参数: alpha,如:

1
pyplot.grid(alpha=0.4)

这个值位于0到1之间。不过如果设置为0,那么线条将不显示(因为完全透明了)

下面是代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from matplotlib import pyplot


a = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
x_names = []

font_style = 'Kaiti'
for i in range(11, 31):
x_names.append('{}岁'.format(i))
print(x_names)

pyplot.figure(figsize=(20, 8), dpi=80)
pyplot.xticks(range(11, 31), x_names, fontproperties=font_style)
pyplot.yticks(a, [str(i) + '个'for i in a], fontproperties=font_style)
pyplot.xlabel('年龄(岁)', fontproperties=font_style)
pyplot.ylabel('数量(个)', fontproperties=font_style, rotation=30)
pyplot.grid()
pyplot.plot(range(11, 31), a)
pyplot.savefig('test_it.png')

下面是效果图:

image-20200608064800929

绘制多条折线

绘制多条折线十分的简单,只要再来一个pyplot()即可。下面是代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from matplotlib import pyplot

a = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
b = [1, 0, 1, 3, 2, 0, 6, 2, 10, 4, 4, 5, 6, 5, 4, 3, 3, 1, 9, 0]
x_names = []

font_style = 'Kaiti'
for i in range(11, 31):
x_names.append('{}岁'.format(i))
print(x_names)

pyplot.figure(figsize=(20, 8), dpi=80)
pyplot.xticks(range(11, 31), x_names, fontproperties=font_style)
pyplot.yticks(a, [str(i) + '个' for i in a], fontproperties=font_style)
pyplot.xlabel('年龄(岁)', fontproperties=font_style)
pyplot.ylabel('数量(个)', fontproperties=font_style, rotation=30)
pyplot.grid(alpha=0.1)
pyplot.plot(range(11, 31), a)
pyplot.plot(range(11, 31), b)
pyplot.savefig('test_it.png')

下面是效果:
image-20200608074453840

添加图例(标注线条)

有的时候无法区分这些线条代表了什么,那么下面是标注线条的方法,你只需要在pyplot.plot(x, y)中加上一个参数label即可。

如:

1
pyplot.plot(range(11, 31), a, label='Myself')

下面是全部代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from matplotlib import pyplot

a = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
b = [1, 0, 1, 3, 2, 0, 6, 2, 10, 4, 4, 5, 6, 5, 4, 3, 3, 1, 9, 0]
x_names = []

font_style = 'Kaiti'
for i in range(11, 31):
x_names.append('{}岁'.format(i))
print(x_names)

pyplot.figure(figsize=(20, 8), dpi=80)
pyplot.xticks(range(11, 31), x_names, fontproperties=font_style)
pyplot.yticks(a, [str(i) + '个' for i in a], fontproperties=font_style)
pyplot.xlabel('年龄(岁)', fontproperties=font_style)
pyplot.ylabel('数量(个)', fontproperties=font_style, rotation=30)
pyplot.grid(alpha=0.1)
pyplot.plot(range(11, 31), a, label='Myself')
pyplot.plot(range(11, 31), b, label="MyClassmate")
pyplot.legend()
pyplot.savefig('test_it.png')

下面是效果:

image-20200608075002242

图例显示中文

如果想要使图例显示中文,你需要在pyplot.legend()中添加参数prop,并且这次传的值不能再是字符串了,应该是一个对象,请看代码:

1
2
3
from matplotlib import font_manager

font_style = font_manager.FontProperties(fname='C:/Windows/Fonts/simkai.ttf')

fname为你的字体的路径。

然后是让图例显示中文:

1
pyplot.legend(prop=font_style)

下面是全部代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from matplotlib import pyplot
from matplotlib import font_manager
a = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
b = [1, 0, 1, 3, 2, 0, 6, 2, 10, 4, 4, 5, 6, 5, 4, 3, 3, 1, 9, 0]
x_names = []

font_style = font_manager.FontProperties(fname='C:/Windows/Fonts/simkai.ttf')
for i in range(11, 31):
x_names.append('{}岁'.format(i))
print(x_names)

pyplot.figure(figsize=(20, 8), dpi=80)
pyplot.xticks(range(11, 31), x_names, fontproperties=font_style)
pyplot.yticks(a, [str(i) + '个' for i in a], fontproperties=font_style)
pyplot.xlabel('年龄(岁)', fontproperties=font_style)
pyplot.ylabel('数量(个)', fontproperties=font_style, rotation=30)
pyplot.grid(alpha=0.1)
pyplot.plot(range(11, 31), a, label='我')
pyplot.plot(range(11, 31), b, label="我的同桌")
pyplot.legend(prop=font_style)
pyplot.savefig('test_it.png')

下面是效果图:

image-20200608075758473

可以看到,显示出中文了。

更改图例位置

在pyplot.legend()中加入loc参数,如loc=”upper right”是右上,loc=”upper left”是左上,loc=”center right”是右中,loc=”lower left”是左下。可以通过查看源码来获取参数。

这里就不举图片例子了。

更改线条样式

改变颜色

可以使用如’red’一样的单词,也可以使用16进制。

1
pyplot.plot(range(11, 31), a, label='我', color='#00FF00')

改变线条样式

‘-‘          solid line style
‘–’         dashed line style
‘-.’         dash-dot line style
‘:’          dotted line style

如:

1
pyplot.plot(range(11, 31), b, label="我的同桌", linestyle='--')

改变线条粗细

设置linewidth参数,如:

1
pyplot.plot(range(11, 31), b, label="我的同桌", linewidth=30)

3.刻度使用字符串并用中文显示

先上全部代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
from matplotlib import pyplot
import random

# a=[random.randint(20, 35) for i in range(120)],a表示10点到12点的每一分钟的气温,如何绘制折线图观察每分钟气温的变化情况?
x = list(range(0, 120))
y = [random.randint(20, 35) for i in range(120)]
pyplot.figure(figsize=(20, 8), dpi=80)
# 列表推导式
x_ticks = ["{}点:{}分".format(i, k) for i in range(10, 12) for k in range(0, 60)]
pyplot.xticks(x[:: 10], x_ticks[:: 10], fontproperties='Kaiti', fontsize=10)
pyplot.plot(x, y)
pyplot.savefig('./template.png')

可以看到,在调用pyplot.xticks时传入了两个列表参数,一个是x, 一个是x_ticks,后面两个参数下面再解释,我们先来说这个x参数和x_ticks参数:如果想要使用字符串刻度,那么需要在传入元素是字符串的列表参数的时候,将元素是数字的列表的原x轴也传入进去。记得保证两者长度相同,不然显示出来的有可能会混乱(比如该显示1的地方显示3,并且乱了你还看不出来)。

1
pyplot.xticks(元素是数字的列表, 元素是字符串的列表)

fontproperties是指定字体,我这里使用的是楷体,似乎只要字体在Windows/Fonts 下就不需要指定字体路径了。fontsize是字体大小。

运行效果:

image-20200528161037559

评论



愿火焰指引你