Python for Finance: Analyze Big Financial Data

(Elle) #1

matplotlib also provides a specific function to generate scatter plots. It basically works in


the same way, but provides some additional features. Figure 5-14 shows the corresponding


scatter plot to Figure 5-13, this time generated using the scatter function:


In  [ 18 ]: plt.figure(figsize=( 7 ,     5 ))
plt.scatter(y[:, 0 ], y[:, 1 ], marker=‘o’)
plt.grid(True)
plt.xlabel(‘1st’)
plt.ylabel(‘2nd’)
plt.title(‘Scatter Plot’)

Figure 5-14. Scatter plot via scatter function

The scatter plotting function, for example, allows the addition of a third dimension,


which can be visualized through different colors and be described by the use of a color


bar. To this end, we generate a third data set with random data, this time with integers


between 0 and 10:


In  [ 19 ]: c   =   np.random.randint( 0 ,   10 ,   len(y))

Figure 5-15 shows a scatter plot where there is a third dimension illustrated by different


colors of the single dots and with a color bar as a legend for the colors:


In  [ 20 ]: plt.figure(figsize=( 7 ,     5 ))
plt.scatter(y[:, 0 ], y[:, 1 ], c=c, marker=‘o’)
plt.colorbar()
plt.grid(True)
plt.xlabel(‘1st’)
plt.ylabel(‘2nd’)
plt.title(‘Scatter Plot’)
Free download pdf