失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python绘制折线图保存_Python系统学习 - 绘制简单折线图

python绘制折线图保存_Python系统学习 - 绘制简单折线图

时间:2023-01-29 14:29:19

相关推荐

python绘制折线图保存_Python系统学习 - 绘制简单折线图

今天我们开始学习 "数据可视化"这一章,我们主要是使用matplotlib库,来实现将数据整理以图表等形式显示出来

数据可视化

1.mpl_squares.py:

# coding = utf8

import os

os.path.abspath(".")

import matplotlib.pyplot as plt

"""绘制简单折线图

"""

input_values = [1, 2, 3, 4, 5]

squares = [1, 4, 9, 16, 25]

plt.title("Square Numbers", fontsize = 14)

plt.xlabel("Value", fontsize = 14)

plt.ylabel("Squares Value", fontsize = 14)

plt.tick_params(axis = "both", labelsize = 14)

# 没有正确绘制图形的平方对应,是因为当前给的平方值是从1开始的,而pyplot是默认从0开始的,所以有偏差,因此添加输入值即可纠正

plt.plot(input_values, squares, linewidth = 14)

plt.show()

效果图:

2.scatter_squares.py:

# coding = utf8

import os

os.path.abspath(".")

import matplotlib.pyplot as plt

"""绘制散点图

"""

# x_values = [1, 2, 3, 4, 5]

# y_values = [1, 4, 9, 16, 25]

# 自动计算数据

x_values = list(range(1, 1001))

y_values = [x ** 2 for x in x_values]

# 设置x,y坐标的范围

plt.axis([0, 1001, 0, 1002001])

# c : 设置散点的颜色,可直接设置颜色名称或使用RGB元组,0~1颜色逐渐变浅

# plt.scatter(x_values, y_values, c = "red", edgecolor = "none", s = 40)

# plt.scatter(x_values, y_values, c = (0.3, 0.8, 1), edgecolor = "none", s = 40)

# c:colormap颜色映射,根据不同情况来使用不同颜色渐变来表示数值的变化规律

# cmap 映射参考见:/gallery/color/colormap_reference.html#sphx-glr-gallery-color-colormap-reference-py

plt.scatter(x_values, y_values, c = y_values, cmap = plt.cm.Dark2, edgecolor = "none", s = 40)

plt.title("Squares number", fontsize = 14)

plt.xlabel("Values", fontsize = 14)

plt.ylabel("Squares Values", fontsize = 14)

plt.tick_paramsq(axis = "both", labelsize = 14)

# 紧凑图表,去除空白多余部分

plt.tight_layout()

# 自动保存图表

plt.savefig("./data_Generator/scatter_squares.png", bbox_inches = "tight")

plt.show()

效果图:

如上是对matplotlib库的pyplot图表类的一个简单使用,下一章我们将讲述“随机漫步”

关注 + 收藏 + 点赞哦,谢谢啦~

如果觉得《python绘制折线图保存_Python系统学习 - 绘制简单折线图》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。