Changing fig size with statsmodel
Asked Answered
D

5

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

Dorrie answered 14/2, 2015 at 15:46 Comment(0)
N
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

Noyes answered 14/2, 2015 at 16:11 Comment(0)
P
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()
Politics answered 3/5, 2019 at 21:8 Comment(1)
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()Kordula
R
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()
Rheinlander answered 14/2, 2015 at 16:15 Comment(2)
That's a nice solution - I'd prefer this one over mine.Noyes
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_pacfPolitics
J
9

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

Jeanett answered 5/11, 2020 at 16:50 Comment(1)
For whatever reason, this is the only solution that worked for me in a Jupyter Notebook, while also using Seaborn plots in other cells.Walloping
T
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);
Toronto answered 13/12, 2020 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.