Python数据分析之Matplotlib学习系列——绘制散点图01

本文介绍如何使用Python的Matplotlib库绘制气温变化图,包括数据导入、字体设置、图像绘制、坐标轴调整等步骤,展示了两种不同月份气温的变化趋势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import matplotlib.pyplot as plt
import matplotlib
import numpy as np

# 全局修改中文
font = {
    'family': 'Microsoft YaHei'
}
matplotlib.rc("font", **font)

# 列出数据
y_3 = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15, 15, 19, 21, 22, 22,
       22,
       23]
y_10 = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17, 20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11, 13,
        12,
        13, 6]

x_3 = np.arange(1, 32)
x_10 = np.arange(35, 66)
X = list(x_3) + list(x_10)

# 求出y_3和y_10中的最大值和最小值
min = min(y_3) if min(y_3) < min(y_10) else min(y_10)
max = max(y_3) if max(y_3) > max(y_10) else max(y_10)

# 绘制图像
plt.figure(figsize=(20, 8), dpi=80)
plt.scatter(x_3, y_3, label="3月")
plt.scatter(x_10, y_10, label="10月")

# 调整x、y轴刻度
y3_label = ["3月{}号".format(i) for i in x_3]
y10_label = ["10月{}号".format(i) for i in x_3]
label = y3_label + y10_label
plt.xticks(X, label, rotation=45)
plt.yticks([i for i in range(min, max + 1)])

# 设置标题
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("标题")

# 设置图例
plt.legend()

# 设置网格
plt.grid()

# 显示图像
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值