Python数据可视化学习(基于matplotlib)

本文使用anaconda进行环境和安装包管理,感兴趣的读者可以参考其他帖子安装matplotlib库

学习简单的plt使用

# -*- coding: utf-8 -*-
# 导入plt库
import matplotlib.pyplot as plt
# 生成最简单的折线图
square = [1, 3, 4, 6, 9]
plt.plot(square)
plt.show()
    

在这里插入图片描述

修改标签文字和线条样式

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt

square = [1, 3, 4, 6, 9, 12, 14, 18]

plt.plot(square, linewidth = 3)		# 这里可以直接指定square为数据,然后该函数会认为横坐标默认为12345...
plt.title("test", fontsize = 16)    # 标题字号大小
plt.xlabel("x", fontsize = 12)      # fontsize指的是x这个坐标轴标题的字号大小
plt.ylabel("y", fontsize = 12)
plt.tick_params(axis = 'both', labelsize = 10)    # 刻度字体大小
plt.show()
    

在这里插入图片描述

指定数据内容

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
input = [0, 1, 2, 3, 4, 5, 6, 7, 8]
square = [1, 3, 0, 4, 6, 9, 12, 14, 18]

plt.plot(input, square, linewidth = 3)  # 设置input为x轴, square为y轴
plt.title("test", fontsize = 16)        # 标题字号大小
plt.xlabel("x", fontsize = 12)          # fontsize指的是x这个坐标轴标题的字号大小
plt.ylabel("y", fontsize = 12)
plt.tick_params(axis = 'both', labelsize = 10)    # 刻度字体大小
plt.show()

在这里插入图片描述

散点图

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
input = [0, 1, 2, 3, 4, 5, 6, 7, 8]
square = [1, 3, 0, 4, 6, 9, 12, 14, 18]

plt.scatter(input, square, s = 20)          # 绘制散点图,s是散点的直径/大小
# plt.plot(input, square, linewidth = 3)  # 设置input为x轴, square为y轴
plt.title("test", fontsize = 16)        # 标题字号大小
plt.xlabel("x", fontsize = 12)          # fontsize指的是x这个坐标轴标题的字号大小
plt.ylabel("y", fontsize = 12)
plt.tick_params(axis = 'both', labelsize = 10)    # 刻度字体大小
plt.show()
    

在这里插入图片描述

批量数据生成

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt

x_values = list(range(1, 1001)) # 这里借助list这个数据结构实现的
y_values = [x**2 for x in x_values]

# 绘制散点图,edgecolor默认是黑色轮廓填充蓝色,s是散点的直径/大小
plt.scatter(x_values, y_values, edgecolor = 'none', s = 2)          
# plt.plot(input, square, linewidth = 3)  # 设置input为x轴, square为y轴
plt.title("test", fontsize = 16)        # 标题字号大小
plt.xlabel("x", fontsize = 12)          # fontsize指的是x这个坐标轴标题的字号大小
plt.ylabel("y", fontsize = 12)
plt.tick_params(axis = 'both', labelsize = 10)    # 刻度字体大小
# plt.show()
plt.savefig('test.png', bbox_inches = 'tight')  #保存图表,其中bbox_inches可忽略,作用是删除图表空域边缘

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值