Python 視覺化

Python 金融入門

Adina Howe

Professor

Matplotlib:視覺化套件

點擊此連結查看更多 Matplotlib 圖庫。

matplotlib

Python 金融入門

matplotlib.pyplot-多樣繪圖函式

import matplotlib.pyplot as plt
Python 金融入門

matplotlib.pyplot-多樣繪圖函式

  • plt.plot()

    • 接受描述要繪製資料的引數
  • plt.show()

    • 在螢幕上顯示圖表
Python 金融入門

使用 pyplot 繪圖

import matplotlib.pyplot as plt
plt.plot(months, prices)
plt.show()
Python 金融入門

繪圖結果

Simple plot2

Python 金融入門

紅色實線

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red')
plt.show()
Python 金融入門

繪圖結果

redline

Python 金融入門

虛線

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')
plt.show()
Python 金融入門

繪圖結果

dash

Python 金融入門

色彩與線型

color
'green' green
'red' red
'cyan' cyan
'blue' blue
linestyle
'-' solid line
'--' dashed line
'-.' dashed dot line
':' dotted

更多關於色彩與線型的說明見此處

Python 金融入門

加入座標標籤與標題

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')

# Add labels
plt.xlabel('Months')
plt.ylabel('Consumer Price Indexes, $')
plt.title('Average Monthly Consumer Price Indexes')

# Show plot
plt.show()
Python 金融入門

繪圖結果

labeled_plot

Python 金融入門

加入額外折線

import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')

# adding an additional line
plt.plot(months, prices_new, color = 'green', linestyle = '--') 

plt.xlabel('Months')
plt.ylabel('Consumer Price Indexes, $')
plt.title('Average Monthly Consumer Price Indexes')
plt.show()
Python 金融入門

繪圖結果

twolines

Python 金融入門

散佈圖

import matplotlib.pyplot as plt
plt.scatter(x = months, y = prices, color = 'red')
plt.show()
Python 金融入門

散佈圖結果

scatterplot

Python 金融入門

一起來練習吧!

Python 金融入門

Preparing Video For Download...