失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > matplotlib01-plot折线图 scatter散点图

matplotlib01-plot折线图 scatter散点图

时间:2021-12-28 21:56:27

相关推荐

matplotlib01-plot折线图 scatter散点图

目录

一、plot绘制折线图一般用法

二、scatter绘制散点图一般用法

记录一下数据可视化的python库matplotlib,研究并纪录一下。

matplotlib.pyplot.subplots函数。subplots可以绘制一个或多个图表。返回变量fig表示整张图片,变量ax表示图片中的各个图表,大多数情况下需要使用ax。

一、plot绘制折线图一般用法

要熟悉库的用法,多用孰能生巧。

import matplotlibimport matplotlib.pyplot as pltx_values = range(1, 6)y_values = [x**2 for x in x_values]# print(plt.style.available)# 使用seaborn绘图样式plt.style.use('seaborn')fig, ax = plt.subplots()ax.plot(x_values, y_values, linewidth=3)# xy轴的标题和图像标题ax.set_xlabel('x values', fontsize=14)ax.set_ylabel('y values', fontsize=14)ax.set_title('squares', fontsize=24)# 刻度文字的大小ax.tick_params(axis='both', labelsize=14)plt.show()

再试一下在同一个图表上绘制多组数据,并凸显数据之间的差别。

import csvimport matplotlib.pyplot as pltfrom datetime import datetime# filename = 'sitka_weather_07-_simple.csv'filename = 'sitka_weather__simple.csv'# 读取csv数据文件with open(filename) as f:reader = csv.reader(f)# 表头不是数据head_row = next(reader)dates, highs, lows= [], [], []for row in reader:# 第2列是日期cur_date = datetime.strptime(row[2], '%Y-%m-%d')# 第5、6列是最高气温、最低气温,注意数字的转换high, low = int(row[5]), int(row[6])dates.append(cur_date) highs.append(high)lows.append(low)plt.style.use('seaborn')# plt.style.use('seaborn-dark')fig, ax = plt.subplots()ax.plot(dates, highs, c='red')ax.plot(dates, lows, c='blue')# 最高、最低温度之间填充颜色凸显温差# 入参一个x数据集,两个y数据集,指定颜色和透明度alphaax.fill_between(dates, highs, lows, facecolor='blue', alpha=0.2)ax.set_xlabel('Date', fontsize=16)ax.set_ylabel('Temperature(F)', fontsize=16)ax.set_title(' Highest-Lowest Temperature')ax.tick_params(axis='both', which='major', labelsize=16)# plt.show()plt.savefig('sitka_weather__simple.png', bbox_inches='tight')

二、scatter绘制散点图一般用法

注意color map的用法,让数据可视化。

import matplotlibimport matplotlib.pyplot as pltimport randomx_values = range(1, 1001)y_values = [x**2 for x in x_values]# 使用seaborn绘图样式plt.style.use('seaborn')fig, ax = plt.subplots()# 使用color map来达到随着值的改变来改变散点颜色# 值越大颜色越深ax.scatter(x_values, y_values, s=10, c=y_values, cmap=plt.cm.Blues,edgecolors='none')# 绘制绿色起点和红色终点ax.scatter(x_values[0], y_values[0], s=20, c='green', edgecolors='none')ax.scatter(x_values[-1], y_values[-1], s=20, c='red', edgecolors='none')# xy轴的标题和图像标题ax.set_xlabel('x values', fontsize=14)ax.set_ylabel('y values', fontsize=14)ax.set_title('square scatter', fontsize=24)# 刻度文字的大小ax.tick_params(axis='both', labelsize=14, which='major')# 刻度的范围ax.axis([-100, 1100, -10000, 1100000])# 显示和保存图片,bbox_inches='tight'表示去掉图片中多余的空白# plt.show()plt.savefig('squares_scatter.png', bbox_inches='tight')

标题scatter函数入参c、cmap,使用color map来达到随着值的改变来改变散点颜色

模拟一下分子的布朗运动,绿色为起点,红色为终点,模拟步数。

import matplotlib.pyplot as pltfrom random import choiceclass RandomWalk:def __init__(self, num_steps=5000, distance=5):# 模拟的步数self.num_steps = num_steps# 每一步最长步长self.distance = distance# 起点为(0, 0)self.x_values = [0]self.y_values = [0]def fill_walk(self):"""布朗运动一下"""while len(self.x_values) < self.num_steps:x_step = self._get_setp()y_step = self._get_setp()# 不能停止不动if x_step == 0 and y_step == 0:continue# 更新位置x_step += self.x_values[-1]y_step += self.y_values[-1]self.x_values.append(x_step)self.y_values.append(y_step)def _get_setp(self):"""随机选择方向和步长返回这一步"""direction = choice([-1, 1])distance = choice(range(0, self.distance))step = direction * distancereturn step if __name__ == '__main__':rw = RandomWalk(num_steps=30000)rw.fill_walk()plt.style.use('dark_background')# 指定窗口尺寸,单位英寸fig, ax = plt.subplots(figsize=(15, 9))point_numbers = range(rw.num_steps)ax.scatter(rw.x_values, rw.y_values, c=point_numbers,cmap=plt.cm.Blues, s=2, edgecolors='none')# 突出起点和终点ax.scatter(0, 0, c='green', s=50, edgecolors='none')ax.scatter(rw.x_values[-1], rw.y_values[-1], c='red', s=50,edgecolors='none')# 隐藏坐标轴ax.get_xaxis().set_visible(False)ax.get_yaxis().set_visible(False)plt.savefig('molecular_move.png', bbox_inches='tight')

模拟分子运动

如果觉得《matplotlib01-plot折线图 scatter散点图》对你有帮助,请点赞、收藏,并留下你的观点哦!

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