Python for Finance: Analyze Big Financial Data

(Elle) #1
plt.grid(True)
plt.legend(loc= 0 )
plt.axis(‘tight’)
plt.xlabel(‘index’)
plt.ylabel(‘value’)

Figure 5-11. Plot with two subplots

The placing of subplots in the a matplotlib figure object is accomplished here by the


use of a special coordinate system. plt.subplot takes as arguments three integers for


numrows, numcols, and fignum (either separated by commas or not). numrows specifies the


number of rows, numcols the number of columns, and fignum the number of the sub-plot,


starting with 1 and ending with numrows * numcols. For example, a figure with nine


equally sized subplots would have numrows=3, numcols=3, and fignum=1,2,...,9. The


lower-right subplot would have the following “coordinates”: plt.subplot(3, 3, 9).


Sometimes, it might be necessary or desired to choose two different plot types to visualize


such data. With the subplot approach you have the freedom to combine arbitrary kinds of


plots that matplotlib offers.


[ 23 ]

Figure 5-12 combines a line/point plot with a bar chart:


In  [ 15 ]: plt.figure(figsize=( 9 ,     4 ))
plt.subplot( 121 )
plt.plot(y[:, 0 ], lw=1.5, label=‘1st’)
plt.plot(y[:, 0 ], ‘ro’)
plt.grid(True)
plt.legend(loc= 0 )
plt.axis(‘tight’)
plt.xlabel(‘index’)
plt.ylabel(‘value’)
plt.title(‘1st Data Set’)
plt.subplot( 122 )
plt.bar(np.arange(len(y)), y[:, 1 ], width=0.5,
color=‘g’, label=‘2nd’)
plt.grid(True)
plt.legend(loc= 0 )
plt.axis(‘tight’)
plt.xlabel(‘index’)
plt.title(‘2nd Data Set’)
Free download pdf