Matlab vs Python 作圖


-- Matlab 作圖示例

x=-3:0.00003:3;
y1=sin(x)./x;

y2=x./sin(x);

plot(x,y1,x,y2);


-- Python 作圖示例

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-3, 3, 0.00003)
y1 = 1/(np.sin(x)) * x
y2 = (np.sin(x)) / x

plt.plot(x, y1, x, y2)
plt.show()
 

 

GeoGebra 工具作圖:

 

 

python 畫普朗克線(黑體輻射):

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0.1, 2, 0.002)
y1=1/x**5/(np.exp(2.2/x)-1)

plt.plot(x, y1)
plt.show()


 

書本示例:

1、條形圖

#!/usr/bin/env_python3
import matplotlib.pyplot as plt
plt.style.use('ggplot')
customers = ['ABC','DEF','GHI','JKL','MNO']
#生成序列:0,1,2,3,4 customers_index
= range(len(customers)) sale_amounts = [127,90,201,111,232] fig=plt.figure() ax1=fig.add_subplot(1,1,1) ax1.bar(customers_index,sale_amounts,align='center',color='darkblue') ax1.xaxis.set_ticks_position('bottom') ax1.yaxis.set_ticks_position('left')
#設置x軸顯示值為customers plt.xticks(customers_index,customers,rotation
=0,fontsize='small') plt.xlabel('Customer Name') plt.ylabel('Sale Amount') plt.title('Sale Amount per Customer')
#保存圖片 plt.savefig(
'bar_plot.png',dpi=400,bbox_inches='tight') plt.show()

 2、直方圖

 

import numpy as np
import matplotlib.pyplot as plt

plt.style.use('ggplot')

#隨機種子,一旦隨機種子參數確定,np.random.randn生成的結果也確定
np.random.seed(19680801)

#生成正態分布數據
mu=100 #正態分布均值點
sigma=15 #正態分布標准差
x=mu1+sigma*np.random.randn(10000) #np.random.randn標准正態分布隨機值

num_bins=50 #histogram組數,即柱子的個數

fig,ax=plt.subplots()

#the histogram of the data
#n 表示每個bin的值
#bins,shape為n+1,bins的邊界
#patcher histogram中每一個柱子
#生成的直方圖面積和為1,即sum(n*(bins[1:]-bins[-1]))==1
n,bins,patches=ax.hist(x,num_bins,density=1,color='darkgreen')

#正態分布擬合曲線
y=((1/(np.sqrt(2*np.pi)*sigma))*np.exp(-0.5*(1/sigma*(bins-mu))**2))
ax.plot(bins,y,'--') #畫直線

plt.xlabel('Abscissa labels') #橫坐標label
plt.ylabel('Probability density') #縱坐標label
ax.set_title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')  #設置標題
ax.xaxis.set_ticks_position('bottom') #設置坐標軸顯示位置
ax.yaxis.set_ticks_position('left')
fig.set_facecolor('cyan')  #設置figure面板顏色(青色)

#plt.savefig('historgram.png',dpi=400,bbox_inches='tight')
plt.show()

 

  

matplotlib 官方文檔:https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

參考書本:《Python 數據分析基礎》陳光欣 譯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM