1. 程式人生 > >Python畫圖練習

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()