【python】matplotlib库的使用

【python】matplotlib库的使用

参考自:嵩天老师的《python数据分析与展示》

1. 折线图

"""
plt.plot(x, y, format_string, **kwargs)
x 					: X轴数据,列表或数组,可选
y 					: Y轴数据,列表或数组
format_string		: 控制曲线的格式字符串,可选,详情请参照最后的说明
**kwargs 			: 第二组或更多(x,y,format_string)
当绘制多条曲线时,各条曲线的x不能省略
"""
import matplotlib.pyplot as plt

plt.plot([0, 2, 4, 6, 8], [3, 1, 4, 5, 2])
plt.xlabel('Time')
plt.ylabel('Grade')
plt.axis([-1, 10, 0, 6])
plt.savefig('1', dpi = 300)
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

a = np.arange(10)
plt.plot(a, a*1.5, a, a*2.5, a, a*3.5, a, a*4.5)
plt.savefig('3', dpi = 100)

plt.show()

在这里插入图片描述

2. 散点图

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
ax.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o')
ax.set_title('Simple Scatter')

plt.savefig('1', dpi = 300)
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

RGBA = [10, 10, 10, 200]
plt.title("气泡图", fontproperties='SimHei', fontsize=12, color=[num / 255 for num in RGBA])
x = 10 * np.random.randn(100)
y = 10 * np.random.randn(100)
size = 10 * np.random.randint(10, 90, 100)
plt.scatter(x, y, s=size, c="red", alpha=0.6)
plt.show()
plt.close()

在这里插入图片描述

3. 直方图

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)
mu, sigma = 100, 20
a = np.random.normal(mu, sigma, size=100)

plt.hist(a, 20, density=1, histtype='stepfilled', facecolor='b', alpha=0.75)
plt.title('Histogram')

plt.savefig('1', dpi = 300)
plt.show()

在这里插入图片描述

4. 饼图

import matplotlib.pyplot as plt
import numpy as np

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)

plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
        shadow=False, startangle=90)
plt.axis('tight')

plt.savefig('1', dpi = 300)
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)

plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
        shadow=False, startangle=90)

plt.savefig('2', dpi = 300)
plt.show()

在这里插入图片描述

5. 子绘图区域

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

a = np.arange(0.0, 5.0, 0.02)

def cos():
    plt.plot(a, np.cos(2*np.pi*a))

plt.subplot2grid((3,3), (0,0), colspan=3)
cos()
plt.subplot2grid((3,3), (1,0), colspan=2)
cos()
plt.subplot2grid((3,3), (1,2), rowspan=3)
cos()
plt.subplot2grid((3,3), (2,0))
cos()
plt.subplot2grid((3,3), (2,1))
cos()

plt.savefig('1', dpi = 300)
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

a = np.arange(0.0, 5.0, 0.02)

def cos():
    plt.plot(a, np.cos(2*np.pi*a), 'r')

gs = gridspec.GridSpec(3,3)

ax1 = plt.subplot(gs[0, :])
cos()
ax1 = plt.subplot(gs[1, :-1])
cos()
ax1 = plt.subplot(gs[1:, -1])
cos()
ax1 = plt.subplot(gs[2, 0])
cos()
ax1 = plt.subplot(gs[2, 1])
cos()

plt.savefig('2', dpi = 300)
plt.show()

在这里插入图片描述

6. 中文显示

import matplotlib.pyplot as plt
import matplotlib

'''
'SimHei'    黑体
'Kaiti'     楷体
'LiSu'      隶书
'FangSong'  仿宋
'YouYuan'   幼圆
'STSong'    华文宋体
'''
matplotlib.rcParams['font.family'] = 'SimHei'
matplotlib.rcParams['font.style'] = 'italic'
matplotlib.rcParams['font.size'] = 20
plt.plot([3,1,4,5,2])
plt.xlabel('时间')
plt.ylabel('成绩')
plt.axis([-1,5,0,6])
plt.savefig('1', dpi = 300)
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np


a = np.arange(0.0, 5.0, 0.02)

plt.xlabel('横轴:时间', fontproperties='SimHei', fontsize=20)
plt.ylabel('纵轴:振幅', fontproperties='SimHei', fontsize=20)
plt.plot(a, np.cos(2*np.pi*a), 'r--')

plt.savefig('2', dpi = 300)
plt.show()

在这里插入图片描述

7. 文本显示

import matplotlib.pyplot as plt
import numpy as np


a = np.arange(0.0, 5.0, 0.02)
plt.plot(a, np.cos(2*np.pi*a), 'r--')

plt.xlabel('横轴:时间', fontproperties='SimHei', fontsize=15, color='green')
plt.ylabel('纵轴:振幅', fontproperties='SimHei', fontsize=15)
plt.title(r'正弦波实例 $y=cos(2\pi x)$', fontproperties='SimHei', fontsize=25)
plt.text(2,1,r'$\mu=100$', fontsize=15)

plt.axis([-1,6,-2,2])
plt.grid(True)

plt.savefig('1', dpi = 300)
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np


a = np.arange(0.0, 5.0, 0.02)
plt.plot(a, np.cos(2*np.pi*a), 'r--')

plt.xlabel('横轴:时间', fontproperties='SimHei', fontsize=15, color='green')
plt.ylabel('纵轴:振幅', fontproperties='SimHei', fontsize=15)
plt.title(r'正弦波实例 $y=cos(2\pi x)$', fontproperties='SimHei', fontsize=25)
plt.annotate(r'$\mu=100$', xy=(2, 1), xytext=(3,1.5),
             arrowprops=dict(facecolor='black', shrink=0.1, width=2))

plt.axis([-1,6,-2,2])
plt.grid(True)

plt.savefig('2', dpi = 300)
plt.show()

在这里插入图片描述

说明

format_string : 控制曲线的格式字符串,可选;由颜色字符、 风格字符和标记字符组成 。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值