【Python】Matplotlib画图

多画布,多子图

#多画布,多子图
import inline as inline
import matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 画第1个图:折线图
x=np.arange(1,100)
plt.subplot(221)
plt.plot(x,x*x)
# 画第2个图:散点图
plt.subplot(222)
plt.scatter(np.arange(0,10), np.random.rand(10))
# 画第3个图:饼图
plt.subplot(223)
plt.pie(x=[15,30,45,10],labels=list('ABCD'),autopct='%.0f',explode=[0,0.05,0,0])
# 画第4个图:条形图
plt.subplot(224)
plt.bar([20,10,30,25,15],[25,15,35,30,20],color='b')
plt.show()

饼图

import matplotlib.pyplot as plt
# 1)准备数据
movie_name = ['语文','数学','英语','物理','化学','生物']

place_count = [50,55,40,35,28,38]

# 2)创建画布
plt.figure(figsize=(15, 6), dpi=80)

# 3)绘制饼图
plt.pie(place_count, labels=movie_name, autopct="%1.2f%%", colors=['b','r','g','y','c','m'])

# 显示图例
plt.legend()

# 添加标题
plt.title("理科1班各科优秀线以上的人数")

# 4)显示图像
plt.axis('equal')
plt.show()

多子图

#多子图
import matplotlib.pyplot as plt

my_dpi = 96
fig, axs = plt.subplots(2, 2, figsize=(480 / my_dpi, 480 / my_dpi), dpi=my_dpi,
                        sharex=True,  # x轴刻度值共享开启
                        sharey=False,  # y轴刻度值共享关闭

                        )
# fig为matplotlib.figure.Figure对象
# axs为matplotlib.axes.Axes,把fig分成2x2的子图
axs[0][0].plot([1, 2, 3])
axs[0][1].bar([1, 2, 3], [4, 5, 6])
axs[0][1].set(title='title')  # 设置axes及子图标题
axs[0][1].set_xlabel('set_xlabel', fontsize=15, color='g')  # 设置x轴刻度标签
axs[0][1].set_ylabel('set_ylabel', fontsize=15, color='g')  # 设置y轴刻度标签
axs[0][1].set_xlim(0, 8)  # 设置x轴刻度范围
axs[0][1].set_xticks(range(0, 10, 2))  # 设置x轴刻度间距
axs[0][1].tick_params(axis='x',  # 可选'y','both'
                      labelsize=20, rotation=45)  # x轴标签旋转、字号等

axs[1][0].plot([1, 2, 3])
axs[1][1].bar([1, 2, 3], [4, 5, 6])

fig.suptitle('DuoZiTu',color='b')  # 设置fig即整整张图的标题

# 修改子图在整个figure中的位置(上下左右)
plt.subplots_adjust(left=0.125,
                    bottom=0.14,
                    right=0.8,  # 防止右边子图y轴标题与左边子图重叠
                    top=0.88,
                    wspace=0.2,
                    hspace=0.2
                    )

plt.show()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Aaron_Liu0730

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

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

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

打赏作者

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

抵扣说明:

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

余额充值