python3使用matplotlib绘制散点图

yipeiwu_com7年前Python基础

本文实例为大家分享了python3使用matplotlib绘制散点图,并标注图例,轴,供大家参考,具体内容如下

代码

from matplotlib import pyplot as plt
from matplotlib import font_manager

# 使得中文可以显示出来
my_font = font_manager.FontProperties(fname="/usr/share/fonts/truetype/arphic/ukai.ttc")
y_3 = [11, 15, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 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 = range(0, 31)
# print(len(y_3))
plt.figure(figsize=(20, 8), dpi=80)
# 使用scatter绘制散点图和绘制折线图的唯一区别
# x,y两个字段的个数应该要能够对应得上
plt.scatter(x, y_3,label="3月份")
x_2 = range(38, 69)
# lable字段可以为图表添加图例
plt.scatter(x_2, y_10,label="10月份")

# 调整x轴刻度
_x = list(x) + list(x_2)
_xtick_labels = ["3月{}日".format(i) for i in x]
_xtick_labels += ["10月{}日".format(i - 37) for i in x_2]
plt.xticks(_x[::3], _xtick_labels[::3], fontproperties=my_font, rotation=45)

# 添加坐标轴描述信息
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度", fontproperties=my_font)
# 设置图表title
plt.title("三月份与十月份温度对比", fontproperties=my_font)
# 添加图例
plt.legend(loc="upper left",prop=my_font)
plt.show()

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python非递归全排列实现方法

刚刚开始学习python,当前看到了函数这一节。结合数组操作,写了个非递归的全排列生成。原理是插入法,也就是在一个有n个元素的已有排列中,后加入的元素,依次在前,中,后的每一个位置插入,...

Python比较配置文件的方法实例详解

工作中最常见的配置文件有四种:普通key=value的配置文件、Json格式的配置文件、HTML格式的配置文件以及YMAML配置文件。 这其中以第一种居多,后三种在成熟的开源产品中较为...

浅析python打包工具distutils、setuptools

浅析python打包工具distutils、setuptools

python中安装包的方式有很多种: 源码包:python setup.py install 在线安装:pip install 包名(linux) / easy_install...

python中对数据进行各种排序的方法

Python列表具有内置的 list.sort()方法,可以在原地修改列表。 还有一个 sorted()内置的函数从迭代构建一个新的排序列表。在本文中,我们将探讨使用Python排序数据...

Python tkinter label 更新方法

Python tkinter label 更新方法

网上看的两个例子关于tkinter界面更新的,简单易懂,分享一下。 例子_1: 代码_1: from tkinter import Tk, Checkbutton, Label f...