您的位置:首页 > 编程语言 > PHP开发

matplotlib.pyplot.subplots

2017-12-14 11:05 489 查看

matplotlib.pyplot.subplots

return:

- fig : matplotlib.figure.Figure object

- ax : Axes object or array of Axes objects.

StackOverflow原话:

plt.subplots() is a function that returns a tuple containing a figure and axes object(s). Thus when using fig, ax = plt.subplots() you unpack this tuple into the variables fig and ax. Having fig is useful if you want to change figure-level attributes or save the figure as an image file later (e.g. with fig.savefig(‘yourfilename.png’). You certainly don’t have to use the returned figure object but many people do use it later so it’s common to see. Also, all axes objects (the objects that have plotting methods), have a parent figure object anyway, thus:

fig, ax = plt.subplots()

is more concise than this:

fig = plt.figure()

ax = fig.add_subplot(111)

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
with tf.Session() as sess:
# 创建子图
fig, ax = plt.subplots()
ax.plot(tf.random_normal([100]).eval(), tf.random_normal([100]).eval(),'o')
ax.set_title("Sample random plot for Tensorflow")
plt.savefig("result.png")
plt.show()


图片自己看吧出不来。

import numpy as np
x = np.linspace(0, 2*np.pi,400)
y = np.sin(x**2)


"""创建只有一个subplot的fig"""
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple Plot')
plt.show()


"""创建两个subplots的fig并解包"""
# 子图划分为1行2列 共享Y轴
f, (ax1, ax2) = plt.subplots(1, 2, sharey= True)
ax1.plot(x,y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
plt.show()


"""创建四个极轴,并通过返回的数组访问它们。"""
# subplot_kw : Dict with keywords passed to the add_subplot()
# add_subplot()的参数polar : If True, equivalent to projection=’polar’.
fig, axes = plt.subplots(2,2,subplot_kw=dict(polar=True))
axes[0, 0].plot(x, y)
axes[1, 1].scatter(x, y)
plt.show()


# 列上共享x轴
plt.subplots(2, 2, sharex='col')
#行上共享Y轴
plt.subplots(2, 2, sharey='row')
#一个坐标轴上画四个图
plt.subplots(2, 2, sharex='all', sharey='all')
#一样效果
plt.subplots(2, 2, sharex=True, sharey=True)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: