matplotlib.pyplot.subplots

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值