码迷,mamicode.com
首页 > 其他好文 > 详细

Pyplot教程(深度学习入门3)

时间:2017-10-06 13:01:24      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:value   sm2   jira   factor   rsh   radius   argument   ATX   ssl   

源地址:http://matplotlib.org/users/pyplot_tutorial.html

 

 

Pyplot tutorial

 

matplotlib.pyplot是一组命令函数集合,使得matplotlib.pyplot像matlab一样工作。每个pyplot函数可以对图片做一些操作,比如:创建图片,在图片中创建绘图区域,在绘图区域中画一些线,在所绘的图上添加属性等等。在matplotlib.pyplot中,pyplot通过函数调用保存各个状态,以便跟踪如当前图形或绘图区域的内容。

In [2]:
import matplotlib.pyplot as plt
import numpy as np
In [3]:
plt.plot([1,2,3,4])
plt.ylabel(‘some numbers‘)
plt.show()
 
技术分享
 

读者也许会感到奇怪,为什么绘制一个从1-4的列表,会给出一个二维的显示。当绘制一个一维列表或序列时,plot函数会认为这是y轴的值加以显示,x轴的值自动生成。由于python从0开始,而给出的数据是1-4共计4个数字,所以x轴的数据是0-3同样也是4个数字.

 

plot()是一组通用命令,可以接受任意的参数,也可以同时接受x与y,例如:plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

 

plot()有第三个可选参数,即可以选择线形以及颜色。所有用于表示颜色和形状的字符源于MATLAB,默认的字符为‘b-’(如果用过matlab肯定知道)。举例来说:

In [4]:
plt.plot([1,2,3,4], [1,4,9,16], ‘ro‘)
plt.axis([0, 6, 0, 20])
plt.show()
 
技术分享
 

上面例子中的axis()中的list参数表示[xmin, xmax, ymin, ymax],同时可以指定轴的可视范围。

 

一般情况下,我们使用numpy提供的数组。事实上,所有的数组都需要内部转换为numpy提供的数组。下面的例子采用一条语句实现不同风格的线。

# evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, ‘r--‘, t, t**2, ‘bs‘, t, t**3, ‘g^‘) plt.show()
 

线的属性

 

可以设置的线属性主要包含有:线宽、虚实等等。线属性的设置方法有如下几种:

 

1.使用关键字参数:plt.plot(x, y, linewidth=2.0)

 

2.使用Line2D实例,plot返回Line2D对象,例如:line1, line2 = plot(x1, y1, x2, y2)。

line, = plt.plot(x, y, ‘-‘) line.set_antialiased(False) # turn off antialising
 

3.获得线属性,使用setp()函数设置

lines = plt.plot(x1, y1, x2, y2) #use keyword args plt.setp(lines, color=‘r‘, linewidth=2.0) #or MATLAB style string value pairs plt.setp(lines, ‘color‘, ‘r‘, ‘linewidth‘, 2.0)
 

Line2D属性的详细属性见:http://matplotlib.org/users/pyplot_tutorial.html

 

操作多线条与多图片

 

Matlab与pyplot针对当前图片或当前轴进行操作,gca()返回当前坐标轴,gcf()返回当前的图片。一般情况下,不用理会这两个操作,下面的操作帮你完成你的任务

In [19]:
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(1)
plt.subplot(211)
plt.plot(t1, f(t1), ‘bo‘, t2, f(t2), ‘k‘)

plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), ‘r--‘)
plt.show()
 
技术分享
 

figure()操作是可选操作,系统默认创建的图像为figure 1。subplot的默认操作为subplot(111)。

 

你可以通过多次调用figure()函数。

In [22]:
plt.figure(1)                # the first figure
plt.subplot(211)             # the first subplot in the first figure
plt.plot([1, 2, 3])
plt.subplot(212)             # the second subplot in the first figure
plt.plot([4, 5, 6])


plt.figure(2)                # a second figure
plt.plot([4, 5, 6])          # creates a subplot(111) by default

plt.figure(1)                # figure 1 current; subplot(212) still current
plt.subplot(211)             # make subplot(211) in figure1 current
plt.title(‘Easy as 1, 2, 3‘) # subplot 211 title
plt.show()
 
技术分享
 
技术分享
 

text()命令可用于在任意位置添加文本,并使用xlabel(),ylabel()和title()在指定位置添加文本

In [23]:
# Fixing random state for reproducibility
np.random.seed(19680801)

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=1, facecolor=‘g‘, alpha=0.75)


plt.xlabel(‘Smarts‘)
plt.ylabel(‘Probability‘)
plt.title(‘Histogram of IQ‘)
plt.text(60, .025, r‘$\mu=100,\ \sigma=15$‘)
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
 
技术分享
 

所有的text()命令返回 matplotlib.text.Text实例。通过传递关键词参数到text(),属性可以被定制。比如:t = plt.xlabel(‘my data‘, fontsize=14, color=‘red‘)

在图片中使用数学表达式:plt.text(60, .025, r‘$\mu=100,\ \sigma=15$‘)
 

annotate()方法可以很方便的提供注解功能,在注释函数中,有两点需要注意:1.注解箭头需要添加的位置,用xy表示,以及注解内容的位置,用xytext表示。添加注解:

In [25]:
ax = plt.subplot(111)

t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = plt.plot(t, s, lw=2)

plt.annotate(‘local max‘, xy=(2, 1), xytext=(3, 1.5),
            arrowprops=dict(facecolor=‘black‘, shrink=0.05),
            )

plt.ylim(-2,2)
plt.show()
 
技术分享
 

对数以及其他非线性表示

 

matplotlib.pyplot不仅支持线性,还支持对数和对数。 如果数据跨越许多数量级,这是常用的方法。 更改很容易:

In [26]:
from matplotlib.ticker import NullFormatter

# Fixing random state for reproducibility
np.random.seed(19680801)

# make up some data in the interval ]0, 1[
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))

# plot with various axes scales
plt.figure(1)

# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale(‘linear‘)
plt.title(‘linear‘)
plt.grid(True)


# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale(‘log‘)
plt.title(‘log‘)
plt.grid(True)


# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale(‘symlog‘, linthreshy=0.01)
plt.title(‘symlog‘)
plt.grid(True)

# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale(‘logit‘)
plt.title(‘logit‘)
plt.grid(True)
# Format the minor tick labels of the y-axis into empty strings with
# `NullFormatter`, to avoid cumbering the axis with too many labels.
plt.gca().yaxis.set_minor_formatter(NullFormatter())
# Adjust the subplot layout, because the logit one may take more space
# than usual, due to y-tick labels like "1 - 10^{-3}"
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
                    wspace=0.35)

plt.show()
 
技术分享
In [ ]:
 

Pyplot教程(深度学习入门3)

标签:value   sm2   jira   factor   rsh   radius   argument   ATX   ssl   

原文地址:http://www.cnblogs.com/hgl0417/p/7631367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!