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 金融入门

绘图结果

简单折线图2

Python 金融入门

红色实线

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

绘图结果

红色线

Python 金融入门

虚线

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

绘图结果

虚线

Python 金融入门

颜色与线型

颜色
'green' 绿色
'red' 红色
'cyan' 青色
'blue' 蓝色
线型
'-' 实线
'--' 虚线
'-.' 点划线
':' 点线

更多颜色与线型文档见此处

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 金融入门

绘图结果

带标签的图

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 金融入门

绘图结果

两条线

Python 金融入门

散点图

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

散点图结果

散点图

Python 金融入门

开始练习吧!

Python 金融入门

Preparing Video For Download...