前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Matplotlib新手上路(中)

Matplotlib新手上路(中)

作者头像
菩提树下的杨过
发布2018-03-28 13:53:27
6990
发布2018-03-28 13:53:27
举报

接上回继续

一、多张图布局(subplot)

1.1 subplot布局方式

import matplotlib.pyplot as plt

plt.figure()

plt.subplot(3, 2, 1)  # 3行2列的第1张图
plt.plot([0, 1], [0, 1])

plt.subplot(322)  # 等效于plt.subplot(2,2,2) 3行2列的第2张图
plt.plot([1, 1], [0, 2])
plt.plot([0, 2], [1, 1])

plt.subplot(3, 1, 2)  # 3行1列的第"2"张图,3行1列的"前提"下,上面一行已占用了1个位置,所以这里是位置2
plt.scatter([0, 1, 2], [1, 1, 1], c="r", s=50)

plt.subplot(3, 3, 7)  # 第3行的第1张图,3行3列的"前提"下,前面二行,已经用掉了6个位置,所以这里是位置7
plt.plot([6, 9], [9, 6])

plt.subplot(3, 3, 8)  # 第3行中间的位置
plt.plot([1, 2], [2, 2])

plt.subplot(3, 3, 9)  # 第3行右侧的位置
plt.plot([1, 3], [2, 4])

plt.show()

上面演示的是“行合并”的布局示例,如果想要“列合并”的效果,参考下面的代码:

import matplotlib.pyplot as plt

plt.figure()

plt.subplot(2, 2, 1)  # 2行2列的位置1
plt.plot([0, 1], [0, 1])
plt.text(0.5, 0, "figure-1", )

plt.subplot(1, 2, 2)  # 1行2列的位置2
plt.plot([0, 1], [0, 1])
plt.text(0.5, 0, "figure-2")

plt.subplot(2, 2, 3)  # 2行2列的位置3
plt.plot([0, 1], [0, 1])
plt.text(0.5, 0, "figure-3")

plt.show()

1.2 subplot2grid布局方式 

这种方式类似于网页制作中的table布局

import matplotlib.pyplot as plt

plt.figure()

ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)  # 3行3列, 第0行0列,合并3列
ax1.text(0.5, 0.5, r"$ax-1$")

ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)  # 3行3列, 第1行0列(即:第二行最左边的位置),合并2列
ax2.text(0.5, 0.5, r"$ax-2$")

ax3 = plt.subplot2grid((3, 3), (2, 0))  # 3行3列, 第1行0列(即:第三行第1个位置)
ax3.text(0.5, 0.5, r"$ax-3$")

ax4 = plt.subplot2grid((3, 3), (2, 1))  # 3行3列, 第2行1列(即:第三行第2个位置)
ax4.text(0.5, 0.5, r"$ax-4$")

ax5 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)  # 3行3列, 第1行2列(即:第二行第3个位置),跨2行
ax5.text(0.5, 0.5, r"$ax-5$")

plt.show()

1.3 gridspec布局方式

这与1.2很类似,只是换一个写法而已

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

gs = gridspec.GridSpec(3, 3)  # 定义3行3列的网络
ax1 = plt.subplot(gs[0:1, 0:3])  # 第0行,[0,3)之间的列合并
ax1.text(0.5, 0.5, r"$ax-1$")

ax2 = plt.subplot(gs[1, :-1])  # 第1行,[0,倒数第1列]之间的列合并
ax2.text(0.5, 0.5, r"$ax-2$")

ax3 = plt.subplot(gs[2, 0])  # 第2行,第0列
ax3.text(0.5, 0.5, r"$ax-3$")

ax4 = plt.subplot(gs[2, 1])  # 第2行,第1列
ax4.text(0.5, 0.5, r"$ax-4$")

ax5 = plt.subplot(gs[1:0, 2])  # [1,最后1列]行合并,第2列
ax5.text(0.5, 0.5, r"$ax-5$")

plt.show()

二、柱状图

import matplotlib.pyplot as plt
import numpy as np

X = [1, 2, 3, 4]
Y1 = [1000, 1500, 1200, 1800]
Y2 = np.array(Y1) * (-1)

plt.bar(X, Y1, 0.4, color="green", label="label1")
plt.bar(X, Y2, 0.4, color="orange", label="label2")

plt.xticks(X)

ax1 = plt.gca()
ax1.set_xticklabels(["Q1", "Q2", "Q3", "Q4"])
ax1.spines['top'].set_color('none')
ax1.spines['right'].set_color('none')

ax1.spines['bottom'].set_position(('data', 0))

plt.legend()

plt.show()

三、3D图

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)

Z = np.sin(X) + np.cos(Y)

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.viridis)

plt.show()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档