Python for Finance: Analyze Big Financial Data

(Elle) #1
Figure 5-17. Stacked histogram for two data sets

Another useful plot type is the boxplot. Similar to the histogram, the boxplot allows both a


concise overview of the characteristics of a data set and easy comparison of multiple data


sets. Figure 5-18 shows such a plot for our data set:


In  [ 23 ]: fig,    ax  =   plt.subplots(figsize=( 7 ,   4 ))
plt.boxplot(y)
plt.grid(True)
plt.setp(ax, xticklabels=[‘1st’, ‘2nd’])
plt.xlabel(‘data set’)
plt.ylabel(‘value’)
plt.title(‘Boxplot’)

This last example uses the function plt.setp, which sets properties for a (set of) plotting


instance(s). For example, considering a line plot generated by:


line    =   plt.plot(data,  ‘r’)

the following code:


plt.setp(line,  linestyle=‘—’)

changes the style of the line to “dashed.” This way, you can easily change parameters after


the plotting instance (“artist object”) has been generated.


Figure 5-18. Boxplot for two data sets

As a final illustration in this section, we consider a mathematically inspired plot that can


also be found as an example in the gallery for matplotlib. It plots a function and

Free download pdf