Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify the appearance of the ACF PLOT function when using subplots #2085

Open
aviPython opened this issue Nov 8, 2014 · 7 comments
Open

Comments

@aviPython
Copy link

Hi!
I'm using statsmodels (0.6.0) on Python 2.7, and i'd like to plot several autocorrelation functions as subplots. Each subplot represents the autocorrelation for different data on each month so i'd like to compare them visually.

Now, the problem is that unlike other functions (which I easily subplot using Matplolib), here I am not able to add a lagend, add xlabel or use other common features in plotting. It seems that ACF plot in statsmodels has fixed and "anchored" properties, like the title "Autocorrelation" cannot be removed (or at least I couldn't find a way). The reason i'd like to remove it is that it appears on the head of each subplot and covers the axis of the plots above it.

Here is the subplots figure that I got:
figure_1

Moreover, It would be helpful to add legends to mark each month and add "Lag" on the bottom of the figure.
the following functions didn't work:

plt.legend(['January'] ,loc='upper right', fontsize = 'x-small')
plt.xticks(visible=False)
plt.xlabel('Lag')

Does anybody know if such changes are feasible and if yes how to do it?

P.S
for the figure above I used the following:

fig, axes = plt.subplots(5,1)
fig = sm.graphics.tsa.plot_acf(ts1['2013/01/01 00:00:00':'2013/01/02 23:59:59'], lags= 86400, ax=axes[0])
fig = sm.graphics.tsa.plot_acf(ts2['2013/02/01 00:00:00':'2013/02/02 23:59:59'], lags= 86400, ax=axes[1])
fig = sm.graphics.tsa.plot_acf(ts3['2013/03/01 00:00:00':'2013/03/02 23:59:59'], lags= 86400, ax=axes[2])
fig = sm.graphics.tsa.plot_acf(ts4['2013/04/01 00:00:00':'2013/04/02 23:59:59'], lags= 86400, ax=axes[3])
fig = sm.graphics.tsa.plot_acf(ts5['2013/05/01 00:00:00':'2013/05/02 23:59:59'], lags= 86400, ax=axes[4])
@josef-pkt
Copy link
Member

You can adjust attributes of the plot using the axes instance, and manipulate the figure with the fig instance
ax.set_title("My Title")
axes[0].legend(['January'] ,loc='upper right', fontsize = 'x-small')
(I don't remember the syntax for the different functions by heart.)

some options can be given to the function in the kwargs like 'markersize'
looking at
http://statsmodels.sourceforge.net/devel/_modules/statsmodels/graphics/tsaplots.html#plot_acf

We can change specific items in the plot, if you have more suggestions.
For example we could add the title to the kwargs.

@aviPython
Copy link
Author

@josef-pkt: thanks for your help!
I used your suggestion and managed to remove the "Autocorrelation'' title by leaving the apprentices empty, using:

axes[0].set_title("")

adding the legend also worked well as you suggested.
I managed finally to add a label to the axis by using "set_":

axes[0].set_xlabel('Lag')

Thanks again!

@josef-pkt
Copy link
Member

(After writing this I realized you already figured it out.)
The axes have set methods. The names are a bit different from the plt.functions. I always need to check.
You need to look at the matplotlib documentation for the details

>>> [i for i in dir(ax) if i.startswith('set_')]
['set_adjustable', 'set_agg_filter', 'set_alpha', 'set_anchor', 'set_animated', 'set_aspect', 
'set_autoscale_on', 'set_autoscalex_on', 'set_autoscaley_on', 'set_axes', 'set_axes_locator', 
'set_axis_bgcolor', 'set_axis_off', 'set_axis_on', 'set_axisbelow', 'set_clip_box', 'set_clip_on', 
'set_clip_path', 'set_color_cycle', 'set_contains', 'set_cursor_props', 'set_figure', 'set_frame_on', 
'set_gid', 'set_label', 'set_lod', 'set_navigate', 'set_navigate_mode', 'set_path_effects', 'set_picker',
 'set_position', 'set_rasterization_zorder', 'set_rasterized', 'set_sketch_params', 'set_snap', 
'set_subplotspec', 'set_title', 'set_transform', 'set_url', 'set_visible', 'set_xbound', 'set_xlabel', 
'set_xlim', 'set_xmargin', 'set_xscale', 'set_xticklabels', 'set_xticks', 'set_ybound', 'set_ylabel', 
'set_ylim', 'set_ymargin', 'set_yscale', 'set_yticklabels', 'set_yticks', 'set_zorder']

@josef-pkt
Copy link
Member

BTW: A PR for improvements would be welcome, if you think some of the options are useful to other users.

@aviPython
Copy link
Author

Sure! the only thing that I can think of now, regarding the ACF plot, is that I think it might be useful to let the user control the colors of the ACF plot (maybe there is a way that I'm not aware of yet), especially the color appearing on the legend. What I mean by that is when you plot a large data set and use a large number of lags, the color of the line changes from BLUE to BLACK. However, the program recognizes the color as blue and therefore, it appears as blue on the legend (blue line next to the string) and could be confusing for the observer, as in my case.
So one option would be to allow an argument for colors in the ACF plot (assuming there isn't such option). Seconds option is maybe to allow the user to cancel out this line that appears in the legend because after all, the ACF plot has only one "line" anyway..

So this option might not be relevant for everyone and might seem too minor, but then again it could be important for some other users.

@josef-pkt
Copy link
Member

adding some plot options sounds like "good as first PR"

@dhpiyush
Copy link
Contributor

dhpiyush commented Jan 5, 2016

@josef-pkt Any other attribute which should be added to the function ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants