前序
在用Python做数据分析的时候,难免会遇到把数据整理成图表,如柱线图,折线图,饼图等。
这里介绍 折线图/点状图 简单用法。
1. 语法
plot([x], y, [fmt], *, data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
根据x,y数据,实现折线或点状图。
x:横坐标数据,可选参数。
y:纵坐标数据。
fmt:可选参数,定义图形基本格式,如颜色、标记和线型。
2. 简单用法
import matplotlib.pyplot as plt
# 这两行代码解决 plt 中文显示的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# X轴坐标值
x_labels = ['2021-2-12', '2021-2-13', '2021-2-14', '2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19']
# Y轴坐标值
y_data1 = [100, 800, 50, 200, 150, 300, 100, 10]
y_data2 = [600, 200, 250, 150, 700, 400, 300, 500]
y_data3 = [50, 300, 200, 100, 1000, 200, 300, 250]
# 默认折线图
plt.plot(x_labels, y_data1)
# 折角是蓝色圆点折线图, fmt=bo
# plt.plot(x_labels, y_data1, 'bo')
# 以Y轴索引为X轴值实现折线图
# plt.plot(y_data1)
# 点状图, fmt=c.
# plt.plot(y_data1, 'c.')
plt.show()
默认折线图 plt.plot(x_labels, y_data1)
#
plt.show()
3. plot支持多个参数,如color(颜色),marker(标记点),linestyle(线条风格),linewidth(线宽),markersize(标记点大小)等。
把这些参数都加进去看一下效果。
plt.plot(x_labels, y_data1, color='green', marker='o', linestyle='dashed',linewidth=2, markersize=12)
image.png
4. x,y数据支持对象输入,如字典(dict),pandas.DataFame,numpy
y_data4 = {"x_labels": ['2021-2-12', '2021-2-13', '2021-2-14',
'2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19'],
"y_test": [600, 200, 250, 150, 700, 400, 300, 500]}
# 获取y_data4字典x_labels为X轴,y_test为Y轴
plt.plot("x_labels", "y_test", data=y_data4)
5. 支持多条图型展示
一般我们做表格时,不仅是一条折线图或点状图,可能会有二条,三条甚至多条,遇到这种情况,我们plot函数支持两种方式实现。
1)多次调用plot函数。
plt.plot(x_labels, y_data1)
plt.plot(x_labels, y_data2)
plt.plot(x_labels, y_data3)
2)原数据为二维数组。
data5 = [['2021-2-12', '2021-2-13', '2021-2-14', '2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19'],
[600, 200, 250, 150, 700, 400, 300, 500],
[50, 300, 200, 100, 1000, 200, 300, 250],
[100, 800, 50, 200, 150, 300, 100, 10]]
# 展示多条折线图2, 若data5是二维数组,其中第一列为X轴,其余列为Y轴
plt.plot(data5[0], data5[1], data5[2], data5[3])
3)指定 [x], y, [fmt] 多个集合。
plt.plot(x_labels, y_data1, 'r--', x_labels, y_data2, '-g', x_labels, y_data3, '--')
6. [fmt]可选参数介绍
fmt = '[marker][line][color]'
这里仅列出部分参考值。
Markers
image.png
Line Styles
image.png
如:
'b' # blue markers with default shape
'or' # red circles
'-g' # green solid line
'--' # dashed line with default color
'^k:' # black triangle_up markers connected by a dotted line
Colors
image.png
附上完整代码
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
# @Date :2021/2/20 11:22
# @Author :wmzhong
# @Email :wmzhong_01@163.com
# @Bolg :https://www.cnblogs.com/wmzhong/
# @jianshu :https://www.jianshu.com/u/2ef83f0891c7
# @Description:折线,点状图
-------------------------------------------------
"""
import matplotlib.pyplot as plt
# 这两行代码解决 plt 中文显示的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# X轴坐标值
x_labels = ['2021-2-12', '2021-2-13', '2021-2-14', '2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19']
# Y轴坐标值
y_data1 = [100, 800, 50, 200, 150, 300, 100, 10]
y_data2 = [600, 200, 250, 150, 700, 400, 300, 500]
y_data3 = [50, 300, 200, 100, 1000, 200, 300, 250]
y_data4 = {"x_labels": ['2021-2-12', '2021-2-13', '2021-2-14',
'2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19'],
"y_test": [600, 200, 250, 150, 700, 400, 300, 500]}
data5 = [['2021-2-12', '2021-2-13', '2021-2-14', '2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19'],
[600, 200, 250, 150, 700, 400, 300, 500],
[50, 300, 200, 100, 1000, 200, 300, 250],
[100, 800, 50, 200, 150, 300, 100, 10]]
# 设置Y轴标题
plt.ylabel("零花钱/元")
# 标题
plt.title("过年得压岁钱调查结果")
# 默认折线图
# plt.plot(x_labels, y_data1)
# 折角是蓝色圆点折线图
# plt.plot(x_labels, y_data1, 'bo')
# 以Y轴索引为X轴值实现折线图
# plt.plot(y_data1)
# 点状图
# plt.plot(y_data1, 'c.', linestyle='dashed')
# plt.plot(x_labels, y_data1, color='green', marker='o', linestyle='dashed',linewidth=2, markersize=12)
# 获取y_data4字典x_labels为X轴,y_test为Y轴
# plt.plot("x_labels", "y_test", data=y_data4)
# 展示多条折线图1
# plt.plot(x_labels, y_data1)
# plt.plot(x_labels, y_data2)
# plt.plot(x_labels, y_data3)
# 展示多条折线图2, 若data5是二维数组,其中第一列为X轴,其余列为Y轴
# plt.plot(data5[0], data5[1], data5[2], data5[3])
# 展示多条折现图3,指定 [x], y, [fmt] 多个集合
# 设置X坐标尺寸大小,不设置会导致X轴显示过于拥挤,适度设置。
plt.tick_params(labelsize=8)
plt.plot(x_labels, y_data1, 'r--', x_labels, y_data2, '-g', x_labels, y_data3, '--')
# 设置图标
plt.legend(labels=["小明", "小红", "小华"])
plt.show()
以上就是对折线图绘画的简单介绍,如果想了解更多,请关注我右栏微信公众号。