Statsmodels Vector Autoregession - Plots Overalapping - matplotlib

I am using vector autoregression from statsmodels:
https://www.statsmodels.org/devel/vector_ar.html
When I call results.plot(), my subplots are overlapping, can someone provide a solution?

It may be too much data to fit nicely on a plot, but give this a try.
call the results.plot() into a figure and then call .tight_layout()
so:
...#other code to get your model fit
fig = results.plot()
fig.tight_layout()
fig
That should let matplotlib figure out the best way to make it all fit.

Related

Adjust matplotlib plot sizes

I am using matplotlib to spy a numpy matrix but its hardly useful because the x axis is very compressed and I cant see anything in the plot.
How can I make the x axis bigger so I can actually see something useful?
This is the code I am using to produce this result. The size of C is 1500x76
plt.spy(world.C)
plt.show(
spy has an aspect parameter for this purpose:
plt.spy(world.C, aspect='auto')

Draw an ordinary plot with the same style as in plt.hist(histtype='step')

The method plt.hist() in pyplot has a way to create a 'step-like' plot style when calling
plt.hist(data, histtype='step')
but the 'ordinary' methods that plot raw data without processing (plt.plot(), plt.scatter(), etc.) apparently do not have style options to obtain the same result. My goal is to plot a given set of points using that style, without making histogram of these points.
Is that achievable with standard library methods for plotting a given 2-D set of points?
I also think that there is at least one hack (generating a fake distribution which would have histogram equal to our data) and a 'low-level' solution to draw each segment manually, but none of these ways seems favorable.
Maybe you are looking for drawstyle="steps".
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
data = np.cumsum(np.random.randn(10))
plt.plot(data, drawstyle="steps")
plt.show()
Note that this is slightly different from histograms, because the lines do not go to zero at the ends.

I want to plot the output of a python file realtime

I have a code which gives me a output. Part of the code is given below. I am trying to plot the output "xin" vs "tstep" in realtime. The code works but it plots xin in a new window each time and its very slow. Please suggest me a way out to plot it faster and plot the data in one plot.
tstep=1
fig=plt.figure()
plt.axis([-300,400,600,0])
x=list()
y=list()
plt.ion()
plt.show()
while tstep<tend+1:
tval=tstep
phase=0
if xin<intfxpos[0]+tan(intfang[0])*t*(tstep-1):
phase=1
acount=acount+1
else:
bcount=bcount+1
x.append(xin)
y.append(tstep-1)
plt.scatter((xin),(tstep-1))
#tstep=tend+1
plt.draw()
time.sleep(0.05)
plt.pause(0.0005)
This thread seems to be very similar to this one. The code you have posted seems to be from one of the answers there, and does not open new windows at each draw for me.
You can get closer to real-time plotting by using matplotlib's animation API. Also in that thread is an example of the animation API at work, with very high FPS. You have to add fig.show() just after the line reading fig,ax = subplots(1,1), and then call run() at the very bottom outside of the function definitions.

Matplotlib annotate doesn't work on log scale?

I am making log-log plots for different data sets and need to include the best fit line equation. I know where in the plot I should place the equation, but since the data sets have very different values, I'd like to use relative coordinates in the annotation. (Otherwise, the annotation would move for every data set.)
I am aware of the annotate() function of matplotlib, and I know that I can use textcoords='axes fraction' to enable relative coordinates. When I plot my data on the regular scale, it works. But then I change at least one of the scales to log and the annotation disappears. I get no error message.
Here's my code:
plt.clf()
samplevalues = [100,1000,5000,10^4]
ax = plt.subplot(111)
ax.plot(samplevalues,samplevalues,'o',color='black')
ax.annotate('hi',(0.5,0.5), textcoords='axes fraction')
ax.set_xscale('log')
ax.set_yscale('log')
plt.show()
If I comment out ax.set_xcale('log') and ax.set_ycale('log'), the annotation appears right in the middle of the plot (where it should be). Otherwise, it doesn't appear.
Thanks in advance for your help!
It may really be a bug as pointed out by #tcaswell in the comment but a workaround is to use text() in axis coords:
plt.clf()
samplevalues = [100,1000,5000,10^4]
ax = plt.subplot(111)
ax.loglog(samplevalues,samplevalues,'o',color='black')
ax.text(0.5, 0.5,'hi',transform=ax.transAxes)
plt.show()
Another approach is to use figtext() but that is more cumbersome to use if there are already several plots (panels).
By the way, in the code above, I plotted the data using log-log scale directly. That is, instead of:
ax.plot(samplevalues,samplevalues,'o',color='black')
ax.set_xscale('log')
ax.set_yscale('log')
I did:
ax.loglog(samplevalues,samplevalues,'o',color='black')

Multiplot with matplotlib without knowing the number of plots before running

I have a problem with Matplotlib's subplots. I do not know the number of subplots I want to plot beforehand, but I know that I want them in two rows. so I cannot use
plt.subplot(212)
because I don't know the number that I should provide.
It should look like this:
Right now, I plot all the plots into a folder and put them together with illustrator, but there has to be a better way with Matplotlib. I can provide my code if I was unclear somewhere.
My understanding is that you only know the number of plots at runtime and hence are struggling with the shorthand syntax, e.g.:
plt.subplot(121)
Thankfully, to save you having to do some awkward math to figure out this number programatically, there is another interface which allows you to use the form:
plt.subplot(n_cols, n_rows, plot_num)
So in your case, given you want n plots, you can do:
n_plots = 5 # (or however many you programatically figure out you need)
n_cols = 2
n_rows = (n_plots + 1) // n_cols
for plot_num in range(n_plots):
ax = plt.subplot(n_cols, n_rows, plot_num)
# ... do some plotting
Alternatively, there is also a slightly more pythonic interface which you may wish to be aware of:
fig, subplots = plt.subplots(n_cols, n_rows)
for ax in subplots:
# ... do some plotting
(Notice that this was subplots() not the plain subplot()). Although I must admit, I have never used this latter interface.
HTH