Python绘图需要下载安装matplotlib模块,它是一个数学绘图库,我们将使用它来制作简单的图表,如折线图和散点图。关于matplotlib模块的下载安装预配置将在后面具体介绍。
(一)绘制折线图
首先,我们先绘制一个简单的折线图
# 调用模块import matplotlib.pyplot as plt squares=[1, 4, 9, 16, 25]x=[1, 2, 3, 4, 5]plt.plot(x, squares)plt.show()
![d7b50452b393af641dfc1c50d2db150b.png](https://i-blog.csdnimg.cn/blog_migrate/651c56c14a8b7ceab985b093821d3a8b.jpeg)
解释:
(1)plt.plot(x, squares)作用是画图,其中x相当于横坐标轴,squaers相当于纵坐标轴;
(2)plt.show()作用是将画好的图显示出来。
接下来,对上图进行进一步的修饰。
import matplotlib.pyplot as pltsquares=[1, 4, 9, 16, 25]x=[1, 2, 3, 4, 5]# 设置线宽plt.plot(x, squares, linewidth=4)# 设置图表标题,并给坐标轴添加标签plt.title("square of 'x'