Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing fig size with statsmodel

I am trying to make QQ-plots using the statsmodel package. However, the resolution of the figure is so low that I could not possibly use the results in a presentation.

I know that to make networkX graph plot a higher resolution image I can use:

plt.figure( figsize=(N,M) )
networkx.draw(G)

and change the values of N and M to attain desirable results.

However, when I try the same method with a QQ-plot from the statsmodel package, it seems to have no impact on the size of the resulting figure, i.e., when I use

plt.Figure( figsize = (N,M) )
statsmodels.qqplot_2samples(sample1, sample2, line = 'r')

changing M and N have no effect on the figure size. Any ideas on how to fix this (and why this method isn't working)?

like image 636
mlg4080 Avatar asked Feb 14 '15 15:02

mlg4080


3 Answers

You can use mpl.rc_context to temporarily set the default figsize before plotting.

import numpy as np
import matplotlib as mpl
from statsmodels.graphics.gofplots import qqplot_2samples

np.random.seed(10)
sample1 = np.random.rand(10)
sample2 = np.random.rand(10)
n, m = 6, 6

with mpl.rc_context():
    mpl.rc("figure", figsize=(n,m))
    qqplot_2samples(sample1, sample2, line = 'r')

plot

like image 127
cel Avatar answered Nov 10 '22 20:11

cel


This is a great solution and works for other plots too - I upvoted it. Here is the implementation for acf and pacf plots.

N, M = 12, 6
fig, ax = plt.subplots(figsize=(N, M))
plot_pacf(df2, lags = 40, title='Daily Female Births', ax=ax)
plt.show()
like image 15
Bryan Butler Avatar answered Nov 10 '22 20:11

Bryan Butler


The qqplot_2samples function has an ax parameter which allows you to specify a matplotlib axes object on which the plot should be drawn. If you don't supply the ax, then a new axes object is created for you.

So, as an alternative to cel's solution, if you wish to create your own figure, then you should also pass the figure's axes object to qqplot_2samples:

sm.qqplot_2samples(sample1, sample2, line='r', ax=ax)

For example,

import scipy.stats as stats
import matplotlib.pyplot as plt
import statsmodels.api as sm

N, M = 6, 5
fig, ax = plt.subplots(figsize=(N, M))
sample1 = stats.norm.rvs(5, size=1000)
sample2 = stats.norm.rvs(10, size=1000)
sm.qqplot_2samples(sample1, sample2, line='r', ax=ax)
plt.show()
like image 10
unutbu Avatar answered Nov 10 '22 22:11

unutbu