22

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)?

mlg4080
  • 413
  • 2
  • 4
  • 8

5 Answers5

25

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

cel
  • 30,017
  • 18
  • 97
  • 117
15

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()
Bryan Butler
  • 1,750
  • 1
  • 19
  • 19
  • yes, it works. I applied to my code in a for loop as well e.g. ##............................................. l = list(diff_stock_prices[:0]) vname = [] for i in l: vname = ('diff_train_' + i) display(vname) # Split the 95% data to train and 5% to test vname = diff_stock_prices[i].iloc[:split] # display(vname) fig, ax = plt.subplots(figsize=(10, 6)) sm.graphics.tsa.plot_acf(vname, ax=ax, lags=30, title= str('ACF - ' + i)) plt.show() – AnoopDixit Jun 20 '22 at 00:01
10

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()
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 1
    That's a nice solution - I'd prefer this one over mine. – cel Feb 14 '15 at 16:16
  • This is a great solution, and it works for other plots like this. Below is the implementation for the autocorrelation (acf) and partial autocorrelation (pacf) plots which are imported as follows: `from statsmodels.graphics.tsaplots import plot_acf, plot_pacf` – Bryan Butler May 03 '19 at 21:07
8

Just use plt.rc("figure", figsize=(16,8)) before plotting.

Levon Minasian
  • 531
  • 1
  • 5
  • 17
  • For whatever reason, this is the only solution that worked for me in a Jupyter Notebook, while also using Seaborn plots in other cells. – Florin Andrei May 28 '22 at 03:45
4

Check this link here.

I used plt.rc()

plt.rc("figure", figsize=(10,6))
sm.graphics.tsa.plot_acf(nifty_50['close_price'], lags=36000);
Tonechas
  • 13,398
  • 16
  • 46
  • 80