PyPlot画图总结

综述

在可视化中需要考虑的基本元素是:画布!

单图

t = np.arange(0., 5., 0.2)
"""
figure用来定义画布的基本属性
"""
plt.figure(1)
"""
画折线图的函数(x,y,line_shape);如果要在一张图中画多条线直接在后面排就行。
"""
plt.plot(t, t, 'r--',t, t**2, 'bs', t, t**3, 'g^')
plt.show()


line1,line2,line3 = plt.plot(t, t, ‘r–’,t, t2, ‘bs’, t, t3, ‘g^’)会返回一个line对象,可以通过这个对象设置每条线的具体属性

一图多子图

def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)

plt.figure("2subplot")
plt.subplot(2,1,1)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

plt.subplot(2,1,2)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
plt.show()

  • subplot(row,column,index)用来定义子图的位置。subplot(2,1,1)等价于subplot(211)表示当前子图画在2行1列的第1个位置(即上方)*

多图多子图

plt.figure(1)                # 编号为1的figure
plt.subplot(211)             # figure1中的第一个子图
plt.plot([1, 2, 3])
plt.subplot(212)             # figure1中的第二个子图
plt.plot([4, 5, 6])
plt.figure(2)                # figure2
plt.plot([4, 5, 6])          # 默认使用subplot(111),此时figure2为当      
plt.show()

图像修饰

plt.title:图片标题
plt.xlabel:横坐标标题
plt.ylabel:纵坐标标题
plt.tick_params:坐标轴字体大小
plt.xticks([],rotation=90):坐标轴字体旋转,**以及坐标轴的刻度**

实际举例

# 展示图像
def plot_image(i, predictions_array, true_label, img):
  predictions_array, true_label, img = predictions_array, true_label[i], img[i]
  plt.grid(False)
  # 定义x轴的刻度,可以设置得到一个均匀的刻度。
  # xticks(locs,[labels],**kwargs)->locs:定义刻度的范围;labels:对应每个刻度的标签;**Kargs:定义每个标签的旋转、颜色等
  plt.xticks([])
  plt.yticks([])
  # 显示图像
  plt.imshow(img, cmap=plt.cm.binary)

  predicted_label = np.argmax(predictions_array)
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'

  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)


# 展示预测结果
def plot_value_array(i, predictions_array, true_label):
  predictions_array, true_label = predictions_array, true_label[i]
  plt.grid(False)
  plt.xticks(range(10))
  plt.yticks([])
  thisplot = plt.bar(range(10), predictions_array, color="#777777")
  # 设置纵坐标的范围
  plt.ylim([0, 1])
  predicted_label = np.argmax(predictions_array)

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')

num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plot_image(i, predictions[i], test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plot_value_array(i, predictions[i], test_labels)
# 自动调整填充整个区域
plt.tight_layout()
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值