Issue
I´d like to create an autocorrelation plot of financial market returns and use statsmodel's plot_acf()
function for that. However, I am trying to alter the color of all the plot elements but my approach only modifies the color of the markers. Though, neither the bars nor the confidence interval receives the color="red"
argument. I am using Python (3.8.3) and Statsmodels (0.12.1).
The following displays a simple code snippet of my current approach to the autocorrelation plot:
# import required package
import pandas as pd
from statsmodels.graphics.tsaplots import plot_acf
# initialize acplot
fig, ax = plt.subplots(nrows=1, ncols=1, facecolor="#F0F0F0")
# autocorrelation subplots
plot_acf(MSCIFI_ret["S&P500"], lags=10, alpha=0.05, zero=False, title=None, ax=ax, color="red")
ax.legend(["S&P500"], loc="upper right", fontsize="x-small", framealpha=1, edgecolor="black", shadow=None)
ax.grid(which="major", color="grey", linestyle="--", linewidth=0.5)
ax.set_xticks(np.arange(1, 11, step=1))
# save acplot
fig.savefig(fname=(plotpath + "test.png"))
plt.clf()
plt.close()
And here comes the corresponding autocorrelation plot itself:
Does anyone know how to deal with that problem? Any ideas would be much appreciated.
Solution
I have the suspicion that they (accidentally?) hardcoded the color for the confidence interval, overruling any changes the user makes (for instance, the edgecolor
of this area can be modified). I did not see in the source code a way to change the color of the CI polygon. rcParams["patch.facecolor"] = "red"
should change the color, alas, it does not. But we can retrospectively change the color of the generated polygon:
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from matplotlib.collections import PolyCollection
#sample data from their website
dta = sm.datasets.sunspots.load_pandas().data
dta.index = pd.Index(sm.tsa.datetools.dates_from_range('1700', '2008'))
del dta["YEAR"]
curr_fig, curr_ax = plt.subplots(figsize=(10, 8))
my_color="red"
#change the color of the vlines
sm.graphics.tsa.plot_acf(dta.values.squeeze(), lags=40, ax=curr_ax, color=my_color, vlines_kwargs={"colors": my_color})
#get polygon patch collections and change their color
for item in curr_ax.collections:
if type(item)==PolyCollection:
item.set_facecolor(my_color)
plt.show()
Update
Given the muddled approach of keywords, kwarg
dictionaries, and retrospective changes, I think the code might be more readable when changing all colors after statsmodels
has plotted the graph:
...
from matplotlib.collections import PolyCollection, LineCollection
...
curr_fig, curr_ax = plt.subplots(figsize=(10, 8))
sm.graphics.tsa.plot_acf(dta.values.squeeze(), lags=40, ax=curr_ax)
my_color="red"
for item in curr_ax.collections:
#change the color of the CI
if type(item)==PolyCollection:
item.set_facecolor(my_color)
#change the color of the vertical lines
if type(item)==LineCollection:
item.set_color(my_color)
#change the color of the markers/horizontal line
for item in curr_ax.lines:
item.set_color(my_color)
plt.show()
Answered By - Mr. T
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.