【Matplotlib】【Python】如何使用matplotlib绘制折现图

目录

1、绘制折线图

2、修改标签文字和线条粗细

3、修改坐标默认值


1、绘制折线图

下面使用matplotlib绘制一个简单的折线图,在对其进行定制,以实现更丰富的数据可视化。

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import matplotlib.pyplot as plt

squares = [1, 4, 8, 16, 25]
plt.plot(squares)
plt.show()

代码解析:

导入模块pyplot,并指定了别名plt,以免反复输入pyplot。实践中一般都这这样命名。

使用plot函数绘制简单的折线图

plt.show()打开matplotlib查看器,并显示绘制的图形。

执行代码结果:


2、修改标签文字和线条粗细

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import matplotlib.pyplot as plt

squares = [1, 4, 8, 16, 25]
plt.plot(squares, linewidth=5)  # 指定线条宽度


# 设置图表标题, 并给坐标轴加上标签
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.show()

 代码解析:

参数linewidth:决定了plot绘制的线条的粗细

函数title():给图表指定标题

参数fontsize:指定字体的大小

函数xlabel()和ylabel():设置坐标轴标题

函数tick_params():设置刻度的样式,指定的实参将影响x轴和y轴上的刻度(axis='both'),并将刻度标记的字号设置为14(labelsize=14)

 


3、修改坐标默认值

plot默认第一个点对应的坐标值为0,如果不想使用默认值,可以给plot提供输入和输出值。

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import matplotlib.pyplot as plt

input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 8, 16, 25]
plt.plot(squares, linewidth=5)  # 指定线条宽度


# 设置图表标题, 并给坐标轴加上标签
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.show()

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值