Python画图练习

import numpy as np
import matplotlib.pyplot as plt
import pylab as pl
%matplotlib inline

'''    #段落注释
plt.figure(1)  #创建图标1
plt.figure(2)   #创建图表2
ax1 = plt.subplot(211)   #在图表2中创建子图1
ax2 = plt.subplot(212)     #在图表2中创建子图2
x = np.linspace(0 , 3 , 100)

for i in range(5):
    plt.figure(1)  #选择图表1
    plt.plot(x , np.exp(i * x/3))
    plt.sca(ax1)  #选择图表2的子图1
    plt.plot(x , np.sin(i*x))
    plt.sca(ax2)
    plt.plot(x , np.cos(i*x))
    
plt.show()

x1 = range(0 , 50)
y1 = [num**2 for num in x1]
x2 = [0,1]
y2 = [0,1]
#pl.plot(x1 , y1)
#pl.plot(x1 , y1 , 'o')   #散点图
pl.plot(x1 , y1 , '--')   #线条样式散点图
pl.show()
Fig = plt.figure(figsize=(8,4))
Ax = Fig.add_subplot(111)
Ax.plot(x1 , y1 , x2 , y2)
Ax2 = Fig.add_subplot(121)
Ax2.plot(x1 ,np.cos(x1))
Fig.show()
Fig.savefig("test.pdf")
'''
 
"""    #段落注释
x = [1, 2, 3, 4, 5]# Make an array of x values
y = [1, 4, 9, 16, 25]# Make an array of y values for each x value
pl.plot(x, y)# use pylab to plot x and y
pl.title('Plot of y vs. x')# give plot a title
pl.xlabel('x axis') #make axis labels
pl.ylabel('y axis')
pl.xlim(0.0, 7.0)# set axis limits
pl.ylim(0.0, 30.)
pl.show()# show the plot on the screen
"""

'''


x1 = [1, 2, 3, 4, 5]# Make x, y arrays for each graph
y1 = [1, 4, 9, 16, 25]
x2 = [1, 2, 4, 6, 8]
y2 = [2, 4, 8, 12, 16]
pl.plot(x1, y1, 'r')# use pylab to plot x and y设置红色
pl.plot(x2, y2, 'g')
pl.title('Plot of y vs. x')# give plot a title
pl.xlabel('x axis')# make axis labels
pl.ylabel('y axis')
pl.xlim(0.0, 9.0)# set axis limits
pl.ylim(0.0, 30.)
pl.show()# show the plot on the screen
'''
'''

#折线图和散点图
x1 = [1, 2, 3, 4, 5]# Make x, y arrays for each graph
y1 = [1, 4, 9, 16, 25]
x2 = [1, 2, 4, 6, 8]
y2 = [2, 4, 8, 12, 16]
p1 ,=pl.plot(x1, y1, 'r')# use pylab to plot x and y设置红色
p2 ,=pl.plot(x2, y2, 'go')
pl.title('Plot of y vs. x')# give plot a title
pl.xlabel('x axis')# make axis labels
pl.ylabel('y axis')
pl.xlim(0.0, 9.0)# set axis limits
pl.ylim(0.0, 30.)
pl.legend([p1 , p2] ,('red line' ,'green circles') , numpoints = 1)                #######
#pl.legend(loc='upper left')
pl.show()# show the plot on the screen
'''
#柱状图
data = np.random.normal(5.0, 3.0, 1000)
# make a histogram of the data array
pl.hist(data)
# make plot labels
#pl.hist(data, histtype='stepfilled')      #没有黑色轮廓
pl.xlabel('data')


pl.show()

你可能感兴趣的:(Python机器学习实践指南)