When drawing time series data etc. with Python, pandas and matplotlib will be used, but the data analysis module pandas has a plot function that incorporates some matplotlib functions, and this pandas.plot and pure ( pure) I thought about how to use matplotlib.plot properly.
Since pandas.plot uses the matplotlib function to implement its functionality (as a wrapper), it is clear that the resulting plot diagram (both pandas.plot and pure matplotlib) will be about the same. The point of proper use is whether it is (subjectively) easy to use or not.
Pandas is good at handling DaraFrame structures, and line plots can be created with almost one command line.
The following data (pandas DataFrame class) was prepared as an example.
In [11]: df_mkt[:10]
Out[12]:
policy actual sp500
2000-01-01 5.5 NaN NaN
2000-01-02 5.5 NaN NaN
2000-01-03 5.5 NaN 1441.46997
2000-01-04 5.5 NaN 1441.46997
2000-01-05 5.5 NaN 1441.46997
2000-01-06 5.5 NaN 1441.46997
2000-01-07 5.5 NaN 1441.46997
2000-01-08 5.5 NaN 1441.46997
2000-01-09 5.5 NaN 1441.46997
2000-01-10 5.5 NaN 1465.15002
Data on US policy rates and effective interest rates from 2000 to 2014, and the S & P 500 index. This is plotted as follows.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
(Omitted)
df_mkt.plot(grid=True, figsize=(10,4), secondary_y=['sp500'])
I used the "secondary_y" option to specify the use of the right axis, but it's very ** easy **.
The decoration of figures such as titles and axis labels has not been completed yet, but it is almost complete. If you try to draw this with matplotlib, you will need the following code.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
(Omitted)
fig, ax1 = plt.subplots(figsize=(10,4))
plt.plot(df_mkt.index, df_mkt['policy'], 'b', label='policy')
plt.plot(df_mkt.index, df_mkt['actual'], 'g', label='actual')
plt.grid(True)
plt.legend(loc=4)
ax2 = ax1.twinx()
plt.plot(df_mkt.index, df_mkt['sp500'], 'r', label='s&p500')
plt.legend(loc=0)
(In this drawing, it is necessary to specify that the vertical axis is divided into the right axis and the left axis.) For the time being, the result of drawing with the above code is attached. (The finish is ** almost ** the same, but the Legend label is not organized. I put it in a place that gives a "misunderstanding" ... Correction required.)
From the above, the recommended usage cases of the two plot functions are as follows.
-In the case of plotting a historical line chart of time series data handled by pandas DataFrame, use pandas.plot () obediently. -For plots other than line charts of time series data, such as scatter plots or more complex charts, use the plain matplotlib plotting function instead of the pandas plot. (Because there are many charts that pandas.plot does not support.)
-"Python for Data Analysis": (O'reilly Media) http://shop.oreilly.com/product/0636920023784.do -"Python for Finance": (O'reilly Media) http://shop.oreilly.com/product/0636920032441.do -Official documentation http://matplotlib.org/contents.html, [http://pandas.pydata.org/pandas-docs/stable/visualization.html] ](Http://pandas.pydata.org/pandas-docs/stable/visualization.html)
Recommended Posts