什么是matplotlib?先体验一下它的功能

入门级使用

体验效果


from matplotlib import pyplot as plt

x = range(2,5)  # 【2,3,4】
y = [1,3,2]

plt.plot(x,y)
plt.show()



知识点



matplotlib 库
pyplot 模块
plot 绘制折线图的方法
show 显示折线图的方法



图片的尺寸可以调整吗?

设置图片的大小 figure



figure(figsize=(宽,高),dpi=80)





MPAndroidChart折线图设置y轴_matplotlib横坐标刻度线过多


from matplotlib import pyplot as plt

x = [1, 2, 3]
y = [10, 30, 26]

plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
plt.show()


绘好的图可以保存吗?

保存绘图 savefig


savefig(图片路径)


from matplotlib import pyplot as plt

x = [1, 2, 3]
y = [10, 30, 26]

plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
plt.show()
plt.savefig('ze.png')


效果

会保存一个图片文件

ze.png

用pycharm看图不方便

打开并弹出指定的图片


from PIL import Image
image = Image.open('plt.png')
image.show()


图片的x与y的刻度不是想要的

自定义刻度 xticks yticks


xticks(数据)
yticks(数据)


注意

数据,参数是刻度显示的数值容器

使用前


MPAndroidChart折线图设置y轴_matplotlib 折线图_02


使用后


MPAndroidChart折线图设置y轴_matplotlib折线图_03


代码


from matplotlib import pyplot as plt

x = [1, 2, 3]
y = [10, 30, 26]

plt.figure(figsize=(20, 8), dpi=80)
plt.xticks(x)
plt.yticks(y)
plt.plot(x, y)
plt.savefig('ze.png')


from PIL import Image
image = Image.open('ze.png')
image.show()


其它情况

刻度数据与实际值,偏差比较大的情况


MPAndroidChart折线图设置y轴_matplotlib 折线图_04


MPAndroidChart折线图设置y轴_jfreechart折线图y轴刻度值_05


刻度可以不要只显示数字吗?

自定义刻度内容


xticks(刻度列表,描述列表)
yticks(刻度列表,描述列表)


效果


MPAndroidChart折线图设置y轴_matplotlib横坐标刻度线过多_06


关键


plt.xticks([1, 2.2, 3], ["no-1", "no-2", "haha_no_3"])


全代码


from matplotlib import pyplot as plt

x = [1, 2, 3]
y = [10, 30, 26]

plt.figure(figsize=(20, 8), dpi=80)
plt.xticks([1, 2.2, 3], ["no-1", "no-2", "haha_no_3"])
plt.yticks(y)
plt.plot(x, y)
plt.savefig('ze.png')

from PIL import Image

image = Image.open('ze.png')
image.show()


但是


MPAndroidChart折线图设置y轴_matplotlib 折线图_07


中文存在问题

同时控制台报错


C:ProgramDataAnaconda3python.exe G:/untitled6/00-matplolib/01-认识.py
C:ProgramDataAnaconda3libsite-packagesmatplotlibbackendsbackend_agg.py:211: RuntimeWarning: Glyph 31532 missing from current font.
  font.set_text(s, 0.0, flags=flags)
C:ProgramDataAnaconda3libsite-packagesmatplotlibbackendsbackend_agg.py:211: RuntimeWarning: Glyph 20010 missing from current font.
  font.set_text(s, 0.0, flags=flags)
C:ProgramDataAnaconda3libsite-packagesmatplotlibbackendsbackend_agg.py:211: RuntimeWarning: Glyph 22079 missing from current font.
  font.set_text(s, 0.0, flags=flags)
C:ProgramDataAnaconda3libsite-packagesmatplotlibbackendsbackend_agg.py:180: RuntimeWarning: Glyph 31532 missing from current font.
  font.set_text(s, 0, flags=flags)
C:ProgramDataAnaconda3libsite-packagesmatplotlibbackendsbackend_agg.py:180: RuntimeWarning: Glyph 20010 missing from current font.
  font.set_text(s, 0, flags=flags)
C:ProgramDataAnaconda3libsite-packagesmatplotlibbackendsbackend_agg.py:180: RuntimeWarning: Glyph 22079 missing from current font.
  font.set_text(s, 0, flags=flags)

Process finished with exit code 0


我想让刻度显示为中文?

中文显示法一 rcParams



主要的

from matplotlib import pyplot as plt

# 设置字体
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号


效果


MPAndroidChart折线图设置y轴_matplotlib折线图_08


代码


from matplotlib import pyplot as plt

# 设置字体
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

x = [1, 2, 3]
y = [10, 30, 26]

plt.figure(figsize=(20, 8), dpi=80)
plt.xticks([1, 2.2, 3], ["第1个","第2个","嘿嘿3"])
plt.yticks(y)
plt.plot(x, y)
plt.savefig('ze.png')

from PIL import Image

image = Image.open('ze.png')
image.show()


中文显示办法二 font_manager

1,创建字体对象

2,显示中文的地方,传入字体对象


# 导入字体的模块
from matplotlib import font_manager

# 实例字体对象
font = font_manager.FontProperties(fname=r'C:WindowsFontsmsyhbd.ttc')


plt.xticks(x,x_tick_labels,fontproperties=font)


效果


MPAndroidChart折线图设置y轴_matplotlib横坐标刻度线过多_09


代码


from matplotlib import pyplot as plt

# 导入字体的模块
from matplotlib import font_manager

# 实例字体对象
font = font_manager.FontProperties(fname=r'C:WindowsFontsmsyhbd.ttc')

x = [1, 2, 3]
y = [10, 30, 26]

plt.figure(figsize=(20, 8), dpi=80)
plt.xticks([1, 2.2, 3], ["第1个","第2个","嘿嘿3"], fontproperties=font)
plt.yticks(y)
plt.plot(x, y)
plt.savefig('ze.png')

from PIL import Image

image = Image.open('ze.png')
image.show()


刻度的文字只能横着吗?

刻度文字角度调整 rotation


plt.xticks(刻度数据,rotation=角度)


效果


MPAndroidChart折线图设置y轴_matplotlib折线图_10


代码


from matplotlib import pyplot as plt

# 导入字体的模块
from matplotlib import font_manager

# 实例字体对象
font = font_manager.FontProperties(fname=r'C:WindowsFontsmsyhbd.ttc')

x = [1, 2, 3]
y = [10, 30, 26]

plt.figure(figsize=(20, 8), dpi=80)
plt.xticks([1, 2.2, 3], ["第1个","第2个","嘿嘿3"], fontproperties=font, rotation=60)
plt.yticks(y)
plt.plot(x, y)
plt.savefig('ze.png')

from PIL import Image

image = Image.open('ze.png')
image.show()


线条是什么意思?横坐标,纵坐标是个什么?

给x与y座标定义标记 给图画定义名称 xlabel ylabel title

效果,中文乱码了


MPAndroidChart折线图设置y轴_matplotlib 折线图_11


使用万能解决方案


MPAndroidChart折线图设置y轴_matplotlib中文文档_12


代码


from matplotlib import pyplot as plt

# 设置字体
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

x = [1, 2, 3]
y = [10, 30, 26]

plt.figure(figsize=(20, 8), dpi=80)
plt.xticks([1, 2.2, 3], ["第1个","第2个","嘿嘿3"], rotation=60)
plt.yticks(y)

plt.xlabel('x坐标')
plt.ylabel('y坐标')
plt.title('我的mmatplolib练习')

plt.plot(x, y)
plt.savefig('ze.png')

from PIL import Image

image = Image.open('ze.png')
image.show()


如果是使用font_manager方式来解决中文问题

在xlabel中添加字体,例如


plt.xlabel("第几局",fontproperties=font)
plt.ylabel("数据",fontproperties=font)
plt.title("折线图测试图",fontproperties=font)


MPAndroidChart折线图设置y轴_matplotlib折线图_13


代码


from matplotlib import pyplot as plt
from matplotlib import font_manager

# 编数据
import random
x = range(1,11)
y = [random.randint(1,10) for i in x]
# 定义字体
font = font_manager.FontProperties(fname=r'C:WindowsFontsmsyhbd.ttc')
# 绘制数据
plt.figure(figsize=(20,8), dpi=80)
plt.plot(x,y)
# 刻度
x_tick_labels = [f'编号{i}' for i in x]
plt.xticks(x,x_tick_labels,rotation=90,fontproperties=font)
# 标记
plt.xlabel("第几局",fontproperties=font)
plt.ylabel("数据",fontproperties=font)
plt.title("折线图测试图",fontproperties=font)
# 保存
plt.savefig('plt.png')
# 打开图片
from PIL import Image
image = Image.open('plt.png')
image.show()


看不清交叉点

显示网格 grip

效果


MPAndroidChart折线图设置y轴_matplotlib 折线图_14


网格颜色太深了,淡一点

设置网络的透明度

效果


MPAndroidChart折线图设置y轴_matplotlib横坐标刻度线过多_15


代码


from matplotlib import pyplot as plt
from matplotlib import font_manager

# 编数据
import random
x = range(1,11)
y = [random.randint(1,10) for i in x]
# 定义字体
font = font_manager.FontProperties(fname=r'C:WindowsFontsmsyhbd.ttc')
# 绘制数据
plt.figure(figsize=(20,8), dpi=80)
plt.plot(x,y)
# 刻度
x_tick_labels = [f'编号{i}' for i in x]
plt.xticks(x,x_tick_labels,rotation=90,fontproperties=font)
# 标记
plt.xlabel("第几局",fontproperties=font)
plt.ylabel("数据",fontproperties=font)
plt.title("折线图测试图",fontproperties=font)
plt.grid(alpha=0.2)
# 保存
plt.savefig('plt.png')
# 打开图片
from PIL import Image
image = Image.open('plt.png')
image.show()


两个或者多个折线图进行数据值的pk效果

多个折线图的绘制

效果


MPAndroidChart折线图设置y轴_matplotlib中文文档_16


代码


from matplotlib import pyplot as plt
from matplotlib import font_manager

# 编数据
import random
x = range(1,11)
y = [random.randint(1,10) for i in x]
y2 = [random.randint(1,10) for i in x]
# 定义字体
font = font_manager.FontProperties(fname=r'C:WindowsFontsmsyhbd.ttc')
# 绘制数据
plt.figure(figsize=(20,8), dpi=80)
plt.plot(x,y)
plt.plot(x,y2)
# 刻度
x_tick_labels = [f'编号{i}' for i in x]
plt.xticks(x,x_tick_labels,rotation=90,fontproperties=font)
# 标记
plt.xlabel("第几局",fontproperties=font)
plt.ylabel("数据",fontproperties=font)
plt.title("折线图测试图",fontproperties=font)
plt.grid(alpha=0.2)
# 保存
plt.savefig('plt.png')
# 打开图片
from PIL import Image
image = Image.open('plt.png')
image.show()


两个线条,哪个是哪个?都分不清楚

给折线添加标记 plot label


plt.plot(x,y,label='x线条')
plt.plot(x,y2, label='y线条')
plt.legend(prop=字体对象)   # 可试试万能中文方案?


效果


MPAndroidChart折线图设置y轴_matplotlib中文文档_17


from matplotlib import pyplot as plt
from matplotlib import font_manager

# 编数据
import random
x = range(1,11)
y = [random.randint(1,10) for i in x]
y2 = [random.randint(1,10) for i in x]
# 定义字体
font = font_manager.FontProperties(fname=r'C:WindowsFontsmsyhbd.ttc')
# 绘制数据
plt.figure(figsize=(20,8), dpi=80)
plt.plot(x,y,label='x线条')
plt.plot(x,y2, label='y线条')
plt.legend(prop=font)
# 刻度
x_tick_labels = [f'编号{i}' for i in x]
plt.xticks(x,x_tick_labels,rotation=90,fontproperties=font)
# 标记
plt.xlabel("第几局",fontproperties=font)
plt.ylabel("数据",fontproperties=font)
plt.title("折线图测试图",fontproperties=font)
plt.grid(alpha=0.2)
# 保存
plt.savefig('plt.png')
# 打开图片
from PIL import Image
image = Image.open('plt.png')
image.show()


线条标记不要放在右上角

控制线条标记的位置 legend loc


plt.legend(prop=font, loc="upper left")


MPAndroidChart折线图设置y轴_jfreechart折线图y轴刻度值_18


效果


MPAndroidChart折线图设置y轴_jfreechart折线图y轴刻度值_19


使用万能字体解决方案,中文更简洁


from matplotlib import pyplot as plt
from matplotlib import font_manager


# 编数据
import random
x = range(1,11)
y = [random.randint(1,10) for i in x]
y2 = [random.randint(1,10) for i in x]
# 设置字体
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

# 绘制数据
plt.figure(figsize=(20,8), dpi=80)
plt.plot(x,y,label='x线条')
plt.plot(x,y2, label='y线条')
plt.legend(loc="upper left")
# 刻度
x_tick_labels = [f'编号{i}' for i in x]
plt.xticks(x,x_tick_labels,rotation=90)
# 标记
plt.xlabel("第几局")
plt.ylabel("数据")
plt.title("折线图测试图")
plt.grid(alpha=0.2)
# 保存
plt.savefig('plt.png')
# 打开图片
from PIL import Image
image = Image.open('plt.png')
image.show()


想要改变线条的颜色

自定义线条颜色 plot color


plt.plot(x,y2, label='y线条', color='yellow')


颜色可以有的给值方式


MPAndroidChart折线图设置y轴_matplotlib 折线图_20


效果


MPAndroidChart折线图设置y轴_jfreechart折线图y轴刻度值_21


代码


from matplotlib import pyplot as plt
from matplotlib import font_manager


# 编数据
import random
x = range(1,11)
y = [random.randint(1,10) for i in x]
y2 = [random.randint(1,10) for i in x]
# 设置字体
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

# 绘制数据
plt.figure(figsize=(20,8), dpi=80)
plt.plot(x,y,label='x线条', color='blue')
plt.plot(x,y2, label='y线条', color='yellow')
plt.legend(loc="upper left")
# 刻度
x_tick_labels = [f'编号{i}' for i in x]
plt.xticks(x,x_tick_labels,rotation=90)
# 标记
plt.xlabel("第几局")
plt.ylabel("数据")
plt.title("折线图测试图")
plt.grid(alpha=0.2)
# 保存
plt.savefig('plt.png')
# 打开图片
from PIL import Image
image = Image.open('plt.png')
image.show()


可以有其它的线条形态吗?

自定义线条形态 plot linestyle


plt.plot(xlinestyle='--')


MPAndroidChart折线图设置y轴_matplotlib 折线图_22


效果


MPAndroidChart折线图设置y轴_matplotlib折线图_23


代码


from matplotlib import pyplot as plt
from matplotlib import font_manager


# 编数据
import random
x = range(1,11)
y = [random.randint(1,10) for i in x]
y2 = [random.randint(1,10) for i in x]
# 设置字体
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

# 绘制数据
plt.figure(figsize=(20,8), dpi=80)
plt.plot(x,y,label='x线条', color='blue', linestyle=':')
plt.plot(x,y2, label='y线条', color='yellow', linestyle='--')
plt.legend(loc="upper left")
# 刻度
x_tick_labels = [f'编号{i}' for i in x]
plt.xticks(x,x_tick_labels,rotation=90)
# 标记
plt.xlabel("第几局")
plt.ylabel("数据")
plt.title("折线图测试图")
plt.grid(alpha=0.2)
# 保存
plt.savefig('plt.png')
# 打开图片
from PIL import Image
image = Image.open('plt.png')
image.show()



其它可绘制的图形


MPAndroidChart折线图设置y轴_matplotlib 折线图_24