python数据可视化之简单绘制简单折线图

python数据可视化之简单绘制简单折线图

1. 首先安装matplotlib

liunx安装命令:

$ sudo apt-get install python3-matplotlib

windows安装命令:

>> pyhon3 -m pip install --user matplotlib

2. 测试matplotlib

(base) C:\Users\EricRay>python3
Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>>

3. matplotlib画廊

​ 要查看使用matplotlib可制作的各种图表,请访问http://matplotlib.org/的示例画廊。单击画廊中的图表,就可查看用于生成图表的代码。

绘制简单折线图示例

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Date    : 2020-11-13 14:33:39
# @Author  : EricRay
# @Email   : [email protected]
# @Link    : https://blog.csdn.net/ericleiy/
# @Description : 绘制简单的折线图

import matplotlib.pyplot as plt

input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.plot(input_values, squares, linewidth=2)
# plt.plot(1, 4, 9, 16, 25)

# 设置图标标题,并给坐标轴加上标签
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)

# 设置刻度标记的大小
plt.tick_params(axis='both', labelsize=14)
plt.axis([0, 6, 0, 30])
plt.show()

效果图:

python数据可视化之简单绘制简单折线图_第1张图片

使用 scatter() 绘制散点图并设置样式

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Date    : 2020-11-13 15:46:30
# @Author  : EricRay
# @Email   : [email protected]
# @Link    : https://blog.csdn.net/ericleiy/
# @Description : 使用 scatter() 绘制散点图并设置样式

import matplotlib.pyplot as plt

# plt.scatter(2, 4, s=40)
# 绘制一系列点
x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]

# 自动计算数据,绘制1000个代码点
x_values = list(range(1, 1000))
y_values = [x**2 for x in x_values]

"""
plt.scatter() 下面三种方式
"""
# 指定颜色
plt.scatter(x_values, y_values, c='red', edgecolors='none', s=40)

# 自定义颜色,值越接近0,指定颜色越深,值越接近1,指定的颜色越浅
plt.scatter(x_values, y_values, c=(0, 0, 0.8), edgecolors='none', s=40)

# 使用颜色映射 colormap,将C值设置为y值列表
plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Reds,
            edgecolors='none', s=40)

plt.title("Squre Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)

plt.tick_params(axis='both', which='major', labelsize=14)

# 设置每个坐标轴的取值范围
plt.axis([0, 1100, 0, 1100000])
plt.show()

效果图:

python数据可视化之简单绘制简单折线图_第2张图片

你可能感兴趣的:(基于python的大数据分析,Python,数据可视化,python,数据分析)