I am using statsmodels.api.tsa.seasonal_decompose to do some seasonal analysis of a time series.
I set it up using
decomp_viz = sm.tsa.seasonal_decompose(df_ts['NetConsumption'], period=48*180)
and then try and visualise it using
decomp_viz.plot()
The output was tiny so I tried to use the standard matplotlib command of
decomp_viz.plot(figsize=(20,20))
However, this got the warning:
TypeError: plot() got an unexpected keyword argument 'figsize'
The documentation says that a matplotlib.figure.Figure is returned from DecomposeResult.plot so I am unsure as to why this error is happening.
My version of statsmodels is 0.13.1 and I am aware that the documentation is for 0.14.0, but conda says that that version does not exist and that I cannot update to it.
Any thoughts would be appreciated.
DecomposeResult.plot doesn't pass keyword arguments. You can change the figure size after you create it:
import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt
PERIOD = 48*180
g = np.random.default_rng(20211225)
y = np.cos(2 * np.pi * np.linspace(0, 10.0, 10*PERIOD))
y += g.standard_normal(y.shape)
decomp_viz = sm.tsa.seasonal_decompose(y, period=PERIOD)
fig = decomp_viz.plot()
fig.set_size_inches((16, 9))
# Tight layout to realign things
fig.tight_layout()
plt.show()
Alternatively, you can do the same by altering the MPL rc.
import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt
# Change default figsize
plt.rc("figure",figsize=(20,20))
PERIOD = 48*180
g = np.random.default_rng(20211225)
y = np.cos(2 * np.pi * np.linspace(0, 10.0, 10*PERIOD))
y += g.standard_normal(y.shape)
decomp_viz = sm.tsa.seasonal_decompose(y, period=PERIOD)
decomp_viz.plot()
plt.show()
which produces (cropped as too big for my screen)
Related
I want to show the value of a 0 or 1 array on a plot with other timeseries.
How can I achieve something like the grey lines below - except mine will oscillate a lot more.
series.
For example, how to add osc here:
import numpy as np
import matplotlib.pyplot as plt
import datetime
import pandas as pd
n = 100
x = range(n)
y = np.random.rand(100)
osc = np.random.randint(2, size=n)
plt.plot(x,y)
plt.show(block=True)
Well, you can loop through the values and call axvspan(x0,x1,color=...,alpha=...);
import numpy as np
import matplotlib.pyplot as plt
n = 100
x = range(n)
y = np.random.randn(100).cumsum()
osc = np.random.randint(2, size=n)
plt.plot(x, y, color='crimson')
for x0, x1, os in zip(x[:-1], x[1:], osc):
if os:
plt.axvspan(x0, x1, color='blue', alpha=0.2, lw=0)
plt.margins(x=0)
plt.show()
Note that only the first 99 values of osc are used, as there are only 99 intervals.
See code below:
import numpy as np
import matplotlib.pyplot as plt
n = 100
x = range(n)
y = np.random.rand(100)
osc = np.random.randint(2, size=n)
fig,ax = plt.subplots()
ax.plot(x,y)
ax.axvspan(0,5,facecolor='grey', alpha=0.4)
plt.show()
Documentation on axvspan can be found here: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axvspan.html.
Similarly you can use axvline for just vertical lines.
I am doing my first matplotlib animation graph. and It's not working.please someone explain me,why??
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
n = 100
X = np.random.randn(n)
def update(curr):
if curr == n:
a.event_source.stop()
plt.cla()
bins = np.arange(-4,4, 0.5)
plt.hist(X[:curr], bin=bins)
plt.axis([-4,4,0,30])
plt.annotate("n={}".format(curr),(3,27))
fig = plt.figure()
a = animation.FuncAnimation(fig, update, interval=100)
P.S. I am coding on jupyter notebook
I got my answer. It's a typo in plt.hist call. The parameter is bins not bin.
plt.hist(X[:curr], bins=bins)
I installed Matplotlib via Anaconda from here: https://anaconda.org/conda-forge/matplotlib
I used the very first command in Anaconda prompt.
But when I tried to plot from python (Spyder) as the following, I get the message:
ModuleNotFoundError: No module named 'matplotlib.plot'
import numpy as np
import matplotlib.plot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x,y)
I have installed numpy, pandas and such using the same method and they work well.
How can I fix this?
Thank you so much.
matplotlib.pyplot is a state-based interface to matplotlib. pyplot is mainly intended for interactive plots and simple cases of programmatic plot generation. Therefore, whenever trying to work with graphs and what is commonly known and informally often referred as matplotlib you should import matplotlib.pyplot as plt:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x,y)
I need to use TkAgg backend in matplotlib to plot on my machine (A) while calculating via ssh in another machine (B).
Following other questions like this, I do:
import matplotlib
matplotlib.use('TkAgg')
matplotlib.get_backend() # prints: 'TkAgg'
import matplotlib.pyplot as plt
matplotlib.get_backend() # prints: 'agg'
# Plotting...
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
plt.plot(t, s)
plt.show()
But gives the error:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
It seems that every time I import matplotlib.pyplot it switches back to the default backend!
What do I need to do?
Is there a way besides the graphical calltip function to obtain unknown x values from y? How would I go about coding an input() to obtain an unknown x value?
import numpy as np
from scipy import interpolate
x = np.linspace(0, 20, 5)
x = np.array([0. , 5. , 10., 15., 20.])
y = np.linspace(0.422,0.948, 5, endpoint =False)
y = np.array([0.422, 0.5513, 0.66433, 0.83433, 0.948])
f = interpolate.interp1d(x,y)
ynew = np.arange(0,1, 0.1)
import matplotlib.pyplot as plt
plt.figure()
plt.plot(x,y,'o', ynew,f(ynew), '-')
Here is the link to a video by APmonitor which had the solution. See script below:
from numpy import *
x = array([0,5,10,15,20])
y = array ([0.422,0.551333,0.66433,0.834333,0.948])
from scipy.interpolate import *
p1 = polyfit(x,y,1)
from matplotlib.pyplot import *
print(p1)
plot(x,y,'o')
plot(x,polyval(p1,x),'r-')
from scipy import *
slope, intercept, r_value, p_value,std_err = linregress(x,y)
print(pow(r_value,2))
print(p_value)