安装matplotlib并在Jupyter Notebook上显示图形


我想使用Jupyter Notebook以一种易于理解的方式查看数据

我正在学习TensorFlow并正在研究API,但是我认为如果可以在图形中看到它将会很方便,因此我尝试使用matplotlib。
它是一种安装方法和简单的使用说明。

以下是与当前环境和TensorFlow相关的文章的链接。

  • 适用于Python初学者的Windows Easy上安装TensorFlow
  • [初学者说明] TensorFlow基本语法和概念
  • [初学者说明] TensorFlow教程MNIST(面向初学者)
  • 使用TensorBoard可视化TensorFlow教程MNIST(面向初学者)
  • TensorFlow API备忘
  • [TensorBoard简介]可视化TensorFlow处理以加深理解

使用Anaconda的pip安装matplotlib

1.从Anaconda Navigator

启动终端

从Windows菜单中启动Anaconda Navigator。从菜单中选择环境,选择虚拟环境,然后通过"打开终端"启动终端。
10.open_terminal_via_anaconda.JPG

2.使用pip

安装matplotlib

只需安装

pip。

1
pip install matplotlib

20.install_matplotlib.JPG

试试matplotlib

按原样尝试Wikipedia的内容。

线图

1
2
3
4
5
6
import matplotlib.pyplot as plt
import numpy as np
a = np.linspace(0,10,100)
b = np.exp(-a)
plt.plot(a,b)
plt.show()

30.matplotlib01.JPG

直方图

1
2
3
4
5
import matplotlib.pyplot as plt
from numpy.random import normal,rand
x = normal(size=200)
plt.hist(x,bins=30)
plt.show()

30.matplotlib02.JPG

散点图

1
2
3
4
5
6
import matplotlib.pyplot as plt
from numpy.random import rand
a = rand(100)
b = rand(100)
plt.scatter(a,b)
plt.show()

30.matplotlib03.JPG

3D图形

很好???

1
2
3
4
5
6
7
8
9
10
11
12
13
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm)
plt.show()

30.matplotlib04.JPG

检查TensorFlow API

使用matplotlib的原始动机是TensorFlow API确认。另外,我正在像这样检查TensorFlow API truncated_normal的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
x = sess.run(tf.truncated_normal([30000], stddev=0.1))
fig = plt.figure()
ax = fig.add_subplot(1,1,1)

ax.hist(x, bins=100)
ax.set_title('Histogram tf.truncated_normal')
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()

30.matplotlib05.JPG