python画图-pylab

使用pylab需要:

import matplotlib.pylab as plt

或者直接:

import pylab as plt

样例1:

plt.figure(figsize=(12, 5.5))  # 宽800 长600
plt.subplot(121)  # 一行两列,这是第一个图
plt.plot(epochs, acc, 'r', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
# plt.figure()  # plt.figure()是新建一个画布。如果有多个图依次可视化的时候,需要使用,否则所有的图都显示在同一个画布中了。
plt.subplot(122)
plt.plot(epochs, loss, 'r', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.savefig('data3/Kuaizi_Classification1_loss={0:.2f}_accur={1:.2f}_epoch={2}.jpg'.format(val_loss[-1], val_acc[-1], epochs[-1]))
# 不在plt.show前保存图片就会使保存的图片空白。':.2f'保留两位小数
plt.show()

样例2:

from keras.datasets import mnist
from keras.utils import to_categorical
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255

plt.subplot(121)  # 一行两列,这是第一个图
plt.imshow(test_images[0].squeeze() * 255)  # squeeze 去掉多余的维度,比如这里是[28,28,1]变为[28,28]
plt.subplot(122)  # 一行两列,这是第二个图
plt.imshow(test_images[1].squeeze() * 255)

plt.show()

画sin x、tan x、ln x、e^x

def graph_math_function():
    fig = plt.figure()  # 新建画布
    ax = axisartist.Subplot(fig, 111)  # 使用axisartist.Subplot方法创建一个绘图区对象ax
    fig.add_axes(ax)  # 将绘图区对象添加到画布中
    ax.axis[:].set_visible(False)
    ax.axis["x"] = ax.new_floating_axis(0, 0, axis_direction="bottom")  # 增加坐标轴
    ax.axis["y"] = ax.new_floating_axis(1, 0, axis_direction="bottom")
    ax.annotate(s='x', xy=(2 * np.pi, 0), xytext=(2 * np.pi, 0.1))  # 增加箭头
    ax.annotate(s='y', xy=(0, 1.0), xytext=(-0.5, 1.0))

    x = np.arange(-np.pi, np.pi, 0.001)
    x1, x2 = [], []
    for i in x:
        if(i < -1.7):
            x1.append(i)
        if(i > 1.7):
            x2.append(i)
    # 这里必须将中间以零为中心的附近的点去掉,不然会为了显示这个函数而压缩其它函数,变得很难看。
    # 并且分成两个区间是为了不让<0的y和>0的y连线

    print(x1)
    # plt.title("y=sin x, y=tan x, y=x")
    # plt.plot(x, np.sin(x), x1, np.tan(x1), x2, np.tan(x2), x, x)
    # plt.title("y=ln(1+x), y=x")
    # plt.plot(x, np.log(1+x), x, x)
    plt.title("y=e^x-1, y=x")
    plt.plot(x, np.exp(x)-1, x, x)


    plt.show()

    # 多个函数画入一图,参考:https://www.cnblogs.com/jmlovepython/p/5673088.html
    # 增加直角坐标系、箭头参考: https://blog.csdn.net/zengbowengood/article/details/102862072

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值