6.18学习笔记(matplotlib,seaborn)

import numpy as np
from matplotlib import pyplot as plt
x=np.arange(1,11)
y=2*x+5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y)
plt.show()

np.arange()函数创建x轴上的值,y轴上的对应值存储在另一个数组对象y中
使用matplotlib软件包的pyplot子模块的plot()函数绘制,图形由show()函数显示
在这里插入图片描述
matplotlib不支持中文,把中文字体放到代码文件夹里

import numpy as np
from matplotlib import pyplot as plt
import matplotlib
#fname为下载的字体库路径,注意SimHei.ttf字体的路径
zhfont1=matplotlib.font_manager.FontProperties(fname="SimHei.ttf")
x=np.arange(1,11)
y=2*x+5
plt.title("pyplot绘图",fontproperties=zhfont1)
#fontproperties设置中文显示,fontsize设置字体大小
plt.xlabel("x轴",fontproperties=zhfont1)
plt.ylabel("y轴",fontproperties=zhfont1)
plt.plot(x,y)
plt.show()

在这里插入图片描述
查看系统自带字体

import matplotlib
a=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
for i in a:
    print(i)

注:[str(i) for i in ls1]这是列表生成式

ls2 =[str(i) for i in ls1]

等价于

ls2 = []
for i in ls1:
        ls2.append(str(i))
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
a=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
for i in a:
    print(i)
#STFangsong(仿宋)
plt.rcParams['font.family']=['STFangsong']
x=np.arange(1,11)
y=2*x+5
plt.title("pyplot绘图")
#fontproperties设置中文显示,fontsize设置字体大小
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.plot(x,y)
plt.show()

在这里插入图片描述

可以向plot()函数添加格式字符串来显示离散值
在这里插入图片描述

在这里插入图片描述

plt.plot(x,y,'ro')

红色圆在这里插入图片描述

在这里插入图片描述

11 0:44:11

发布了5 篇原创文章 · 获赞 2 · 访问量 2672

猜你喜欢

转载自blog.csdn.net/weixin_43621813/article/details/92798777