python 数据可视化 – Python量化投资

python 数据可视化

https://www.matplotlib.org.cn/tutorials/introductory/pyplot.html

1

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2,100)#从0到2,分100步
plt.plot(x,x,label="linear")#曲线类型及取名
plt.plot(x,x**2,label="quadratic")
plt.plot(x,x**3,label="cubic")
plt.xlabel('x label')#坐标轴的名字
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend() #没有它label不显示
plt.show()


image.png

2

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,10,0.2)#从0到10,每步范围为0.2
y = np.sin(x)
fig = plt.figure()#显示一个板
ax = fig.add_subplot(111)#它能调整显示的大小
ax.plot(x,y)#上三步等同于 plt.plot(x,y)
plt.show()


image.png

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,10,0.4)#从0到10,每步范围为0.2
plt.plot(x,x,'r--',x,x**2,'bs',x,x**3,'g^')#看图吧,不明白
plt.show()


image.png

import matplotlib.pyplot as plt
import numpy as np
name = [1,2,3]
value = [1,10,100]
#设置画布大小
plt.figure(1,figsize=(9,3))
#画出3幅图,分别设置
plt.subplot(131)
plt.bar(name,value)
plt.subplot(132)
plt.scatter(name,value)
plt.subplot(133)
plt.plot(name,value)
plt.suptitle("Categorical Plotting")
plt.show()


image.png

import matplotlib.pyplot as plt
import numpy as np
data = {'a' : np.arange(50),
      'c': np.random.randint(0,50,50),
      'd': np.random.randn(50)}
data['b'] = data['a']+10*np.random.randn(50)
data['d'] = np.abs(data['d'])*100
#x坐标 数组a, y坐标 数组b,颜色c 数组c,大小s 数组d
plt.scatter('a','b',c='c',s='d',data=data)
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()


image.png

import matplotlib.pyplot as plt
plt.figure(1)                # the first figure
plt.subplot(211)             # the first subplot in the first figure
plt.plot([1, 2, 3])
plt.subplot(212)             # the second subplot in the first figure
plt.plot([4, 5, 6])
plt.figure(2)                # a second figure
plt.plot([4, 5, 6])          # creates a subplot(111) by default
plt.figure(1)                # figure 1 current; subplot(212) still current
plt.subplot(211)             # make subplot(211) in figure1 current
plt.title('Easy as 1, 2, 3') # subplot 211 title
plt.show()


image.png

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
data = np.random.randn(2, 100)
fig, axs = plt.subplots(2, 2, figsize=(5, 5))
axs[0, 0].hist(data[0])
axs[1, 0].scatter(data[0], data[1])
axs[0, 1].plot(data[0], data[1])
axs[1, 1].hist2d(data[0], data[1])
plt.show()


image.png

import matplotlib.pyplot as plt
import numpy as np
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=True, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')   # 支持 LaTex格式
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()


image.png
https://www.jianshu.com/p/23870a029ab2

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
pandas
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论