(1-2)matplotlib库绘图

简单题要
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.绘图区域

①绘图区域plt.subplot()
在这里插入图片描述
在这里插入图片描述
②绘图区域plt.subplots()

1、单行单列

import  matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(12, 8), nrows=2, ncols=1) 


ax[0].plot([1,2], [3,4])
# 第二个图为
ax[1].plot([1,2], [3,4])

在这里插入图片描述
2、多行多列

import  matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(12, 8), nrows=2, ncols=2) 

# 第一个图为
ax[0,0].plot([1,2], [3,4])
# 第二个图为
ax[0,1].plot([1,2], [3,4])
# 第三个图为
ax[1,0].plot([1,2], [3,4])
# 第四个图为
ax[1,1].plot([1,2], [3,4])

在这里插入图片描述
在这里插入图片描述

fig, ax = plt.subplots(figsize=(12, 8), nrows=3, ncols=1)
返回值
fig:matplotlib.figure.Figure 对象
ax:子图对象( matplotlib.axes.Axes)或者是他的数组

plt.subplots()设定了子窗口的行、列后,一次性生成所有子窗口
随后可用ax[i].plot()、ax[i].scatter()、ax[i].pie()等命令在第i+1个子窗口中绘图
当只创建了一个窗口时,则直接用ax.plot()、ax.scatter()、ax.pie()等命令,无需索引

在这里插入图片描述

# 导入相关模块
import numpy as np
import matplotlib.pyplot as plt

# 使中文正常显示的参数设置
plt.rcParams['font.sans_serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig, ax = plt.subplots(figsize=(12, 8), nrows=3, ncols=1)  # figsize可设定子图的长宽  
ax[0].plot([1,2,3], [2,4,6])                               # 在第1个子窗口绘图
ax[1].plot([1,2,3], [2,4,6])                               # 在第2个子窗口绘图
ax[2].plot([1,2,3], [2,4,6])                               # 在第3个子窗口绘图

等价于

plt.subplot(3, 1, 1)            # 缩写为plt.subplot(311),表示绘图子窗口被设定为3行1列,当前在第1个窗口
plt.plot([1, 2, 3], [2, 4, 6])  # 在第1个子窗口绘图

plt.subplot(3, 1, 2)            # 缩写为plt.subplot(312),表示绘图子窗口被设定为3行1列,当前在第2个窗口
plt.plot([1, 2, 3], [2, 4, 6])  # 在第2个子窗口绘图

plt.subplot(3, 1, 3)            # 缩写为plt.subplot(313),表示绘图子窗口被设定为3行1列,当前在第3个窗口
plt.plot([1, 2, 3], [2, 4, 6])  # 在第3个子窗口绘图

在这里插入图片描述
③subplot2grid
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.绘制折线图—pyplot函数

①pyplot的参数
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
②matplotlib.pyplot的使用
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
③pyplot的中文显示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
④文本显示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
⑤pyplot绘制折线图

  • 绘制一条折线图
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文

x = [1, 2, 3, 4]
y = [10, 50, 20, 100]
plt.plot(x, y, "r", marker='*', ms=10, label="a")# "r" 表示红色,ms用来设置*的大小,显示标签a

# 在折线图(x1,y1+1)上显示具体数值str(y1), ha参数控制水平对齐方式, va控制垂直对齐方式
for x1, y1 in zip(x, y):
    plt.text(x1, y1 + 1, str(y1), ha='center', va='bottom', fontsize=15)

plt.xlabel("发布日期")
plt.ylabel("小说数量")
plt.title("80小说网活跃度")
plt.legend(loc="upper left")# upper left 将图例a显示到左上角
    
plt.savefig("a.jpg")
plt.show()

在这里插入图片描述

  • 绘制多条折线图
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文

x = [1, 2, 3, 4]
y1 = [45, 50, 20, 100]
y2 = [26, 10, 76, 25]
y3 = [11, 66, 55, 88]
y4 = [69, 50, 35, 100]
plt.plot(x, y1, marker='*', ms=10, label="a")
plt.plot(x, y2, marker='*', ms=10, label="b")
plt.plot(x, y3, marker='*', ms=10, label="c")
plt.plot(x, y4, marker='*', ms=10, label="d")
#等价于plt.plot(x, y1, x,y2, x,y3, marker='*', ms=10, label="a")

# 在折线图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式
for y in [y1, y2, y3, y4]:
    for x1, yy in zip(x, y):
        plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=15)

plt.xlabel("发布日期")
plt.ylabel("小说数量")
plt.title("80小说网活跃度")
plt.legend(loc="upper left")

plt.savefig("a.jpg")
plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
 
x = np.linspace(0, 2 * np.pi, 100)#返回0到2派之间均匀的100个数值
y1, y2 = np.sin(x), np.cos(x)

 
plt.plot(x, y1, label='y = sin(x)')
plt.plot(x, y2, label='y = cos(x)')
plt.legend(loc="lower left")# 可以选loc=‘upper right' 或‘upper left'或 ‘lower right'或 ‘lower left'
plt.show()

在这里插入图片描述

2.绘制饼图

饼状图函数

plt.pie(x, explode=None, labels=None, colors=None, startangle=None,autopct=None, shadow=False, radius=None)在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import matplotlib.pyplot as plt
x = [10,20,50,30]
label = ['A','B','C','D']
explods = [0,0.2,0,0]
plt.pie(x,labels=label,shadow=True,autopct='%1.1f%%',explode=explods,startangle=90)
#autopct='%1.1f%%'是必备的条件,加上它才能显示百分数;shadow是阴影
plt.legend(loc = (1,1))

在这里插入图片描述

3.绘制直方图

  • matplotlib.pyplot.hist(x,bins=None,range=None,density=None,normed=None,rwidth=None,log=False,color=None,label=None)参数的解释

  • bins:可以为整数,也可以为一个序列(比如list)
    (1) 当bin为整数时,则等于柱子的个数,有bin + 1个边。
    (2)当bin为sequence时,即给定了每个柱子的边界值,柱子个数等于len(sequence) - 1,每个区间,为前闭后开([)),但是最后一个区间为前后闭,如bin = [1, 2, 3, 4],则区间分别为[1,2), [2,3),[3,4]
    在这里插入图片描述

  • range: 即对做hist的x的范围进行限定,默认的range=(x.min(), x.max())
    在这里插入图片描述
    在这里插入图片描述

  • density=True就相当于normed=1,在新的版本中normed被取消,用density代替

  • log : bool,默认False,即y坐标轴是否选择指数刻度
    在这里插入图片描述

  • rwidth ** 柱子的相对宽度

  • 在这里插入图片描述

  • 在这里插入图片描述

  • label 数据的标签,用于展示图例时使用。
    在这里插入图片描述

  • 返回值
    (你可能不明白,为什么有的图是"n, bins, patches = plt.hist(x, 50, density=True, facecolor=‘g’, alpha=0.75) " )
    这三个返回值都是tuple
    第一个值:每个bin的频率(density = True)或频数(density= False)
    第二个值:所有bin的边界值,值的个数为bin_num + 1
    可以利用第一和第二返回值进行绘制曲线拟合
    第三个值:图形的对象。Patches <a list of 2 Lists of Patches objects>

  • EX:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(19680801)
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
plt.hist(x, 50, density=True, facecolor='g', alpha=0.75)
#density=True就相当于normed=1,在新的版本中normed被取消,用density代替;facecolor就是color就是柱子的颜色

plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

c=[1, 1, 1, 2, 2, 1, 2, 2, 3, 3, 1, 2, 2, 3, 3, 1, 2, 2, 1, 1, 2, 3, 3, 1, 1, 2, 3, 3, 1, 1]
d=[-0.5,0.5,1.5,2.5,3.5]
plt.hist(c,bins=d)

a=[1 1 1 2 2 1 2 2 3 3 1 2 2 3 3 1 2 2 1 1 2 3 3 1 1 2 3 3 1 1]
b=[-0.5 0.5 1.5 2.5 3.5]
plt.hist(a,bins=d)

a=np.array(c)
b=np.array(d)
plt.hist(c,bins=d)

'''
bins是一个数字时就是直方的个数,bins是一个数组时就代表横坐标;
plt.hist(a,bins=d)含义就是以d为横坐标,根据a落在d各个区间的数量来绘制直方图
这三种都能绘制直方图,而且结果一样;
不过a=[1 1 1 2 2 1 2 2 3 3 1 2 2 3 3 1 2 2 1 1 2 3 3 1 1 2 3 3 1 1]这是一种非法输入,a=np.array(c)才行
'''

'''
没有逗号的数组是一种非法输入,如a=[1 1 1 2 2 1 2 2 3 3 1 2 2 3 3 1 2 2 1 1 2 3 3 1 1 2 3 3 1 1]
但是我们可以用np.array([1, 1, 1, 2, 2, 1, 2, 2, 3, 3, 1, 2, 2, 3, 3, 1, 2, 2, 1, 1, 2, 3, 3, 1, 1, 2, 3, 3, 1, 1])来得到这种数组
'''

在这里插入图片描述

4.绘制柱状图

  • 绘制单组柱状图
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文

x = [1, 2, 3, 4]
y = [450, 500, 200, 1000]

plt.bar(x=x, height=y, label='书库大全', color='steelblue', alpha=0.8)

# 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式
for x1, yy in zip(x, y):
    plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20)

plt.title("80小说网活跃度")
plt.xlabel("发布日期")
plt.ylabel("小说数量")
plt.legend()# 显示图例

plt.savefig("a.jpg")
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import matplotlib
# 设置matplotlib正常显示中文和负号
matplotlib.rcParams['font.sans-serif']=['SimHei']
matplotlib.rcParams['axes.unicode_minus']=False     


plt.figure(figsize=(20, 8), dpi=250)
movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它']
y=[73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]
x=range(len(movie_name))

plt.bar(x,y,width=0.5, color=['b','r','g','y','c','m','y','k','c','g','g'])
plt.xticks(x, movie_name)#这句话可以使横坐标由range(len(movie_name))替换为电影的名字

plt.show()

在这里插入图片描述

  • 绘制多组柱状图
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文

x = ['2015', '2016', '2017', '2018', '2019']
y1 = [4500, 5000, 2000, 7000, 10000]
y2 = [5200, 7000, 5000, 9000, 11000]

plt.bar(x=x, height=y1, label='python', color='steelblue', alpha=0.8)
plt.bar(x=x, height=y2, label='java', color='indianred', alpha=0.8)

# 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式
for x1, yy in zip(x, y1):
    plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20)
for x1, yy in zip(x, y2):
    plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20)

plt.title("python与java图书对比")
plt.xlabel("年份")
plt.ylabel("销量")
plt.legend()

plt.savefig("a.jpg")
plt.show()

在这里插入图片描述

import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文


years = ['2015', '2016', '2017', '2018', '2019']
y1 = [4500, 5000, 2000, 7000, 10000]
y2 = [5200, 7000, 5000, 9000, 11000]
x=range(len(years))


# 将X轴数据改为使用range(len(years), 就是0、1、2...
plt.bar(x, height=y1, label='python', color='steelblue', alpha=0.8, width=0.2)
plt.bar([i + 0.2 for i in x], height=y2, label='java', color='indianred', alpha=0.8, width=0.2)
plt.xticks([i + 0.1 for i in x],years)#这句话可以使横坐标由range(len(years))+0.1替换为years

# 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式
for x1, yy in enumerate(y1):
    plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20)
for x1, yy in enumerate(y2):
    plt.text(x1 + 0.2, yy + 1, str(yy), ha='center', va='bottom', fontsize=20)

plt.title("python与java对比")
plt.xlabel("年份")
plt.ylabel("销量")
plt.legend()

plt.savefig("a.jpg")
plt.show()

在这里插入图片描述

plt.figure(figsize=(20, 8), dpi=80)

movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案']
first_day = [10587.6, 10062.5, 1275.7]
first_weekend = [36224.9, 34479.6, 11830]

# 先得到movie_name长度, 再得到下标组成列表
x = range(len(movie_name))

plt.bar(x, first_day, width=0.2)
# 向右移动0.2, 柱状条宽度为0.2
plt.bar([i + 0.2 for i in x], first_weekend, width=0.2)

# 底部汉字移动到两个柱状条中间(本来汉字是在左边蓝色柱状条下面, 向右移动0.1)
plt.xticks([i + 0.1 for i in x], movie_name)
plt.show()

在这里插入图片描述

  • 绘制柱线混合图
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文

x = [2, 4, 6, 8]
y = [450, 500, 200, 1000]

plt.bar(x=x, height=y, label='书库大全', color='steelblue', alpha=0.8)
# 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式
for x1, yy in zip(x, y):
    plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20)
    
plt.title("80小说网活跃度")
plt.xlabel("发布日期")
plt.ylabel("小说数量")
plt.legend()

# 画折线图
plt.plot(x, y, "r", marker='*', ms=10, label="a")
plt.legend(loc="upper left")

plt.savefig("a.jpg")
plt.show()

在这里插入图片描述

5.绘制散点图

在这里插入图片描述

'''极其必要的语句'''
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus']=False #这个语句极为重要,它决定能否正常显示负号


'''绘制散点图'''
x = np.arange(1,10)
y = x
plt.scatter(x,y,c='r',marker='o')



'''直方图内你想要显示的内容'''
plt.title("Y-X")
plt.xlabel("X轴")
plt.ylabel("Y轴")
#plt.legend()# 显示图例
#plt.text(0,750, r'$\80小说网活跃度$') #添加文本内容



'''关于其他限定因素'''
plt.axis([0, 10, 0, 10])  
plt.grid(True) #显示网格
plt.savefig("a.jpg",dpi=600)


'''最后'''
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

'''极其必要的语句'''
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus']=False #这个语句极为重要,它决定能否正常显示负号


'''绘制散点图'''
x = np.arange(1,10)
y = x
color=['r','y','g','b','r','y','g','b','r']
plt.scatter(x,y,c=color,marker='o')



'''直方图内你想要显示的内容'''
plt.title("Y-X")
plt.xlabel("X轴")
plt.ylabel("Y轴")
#plt.legend()# 显示图例
#plt.text(0,750, r'$\80小说网活跃度$') #添加文本内容



'''关于其他限定因素'''
plt.axis([0, 10, 0, 10])  
plt.grid(True) #显示网格
plt.savefig("a.jpg",dpi=600)


'''最后'''
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

'''极其必要的语句'''
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus']=False #这个语句极为重要,它决定能否正常显示负号


'''绘制散点图'''
x = np.arange(1,10)
y = x
lValue=x*3
plt.scatter(x,y,c='r',linewidths=lValue,marker='o')



'''直方图内你想要显示的内容'''
plt.title("Y-X")
plt.xlabel("X轴")
plt.ylabel("Y轴")
#plt.legend()# 显示图例
#plt.text(0,750, r'$\80小说网活跃度$') #添加文本内容



'''关于其他限定因素'''
plt.axis([0, 10, 0, 10])  
plt.grid(True) #显示网格
plt.savefig("a.jpg",dpi=600)


'''最后'''
plt.show()

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘风破浪的牛马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值