I want to plot the output of a python file realtime - matplotlib

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.

Related

Turn off x-axis marginal distribution axes on jointplot using seaborn package

There is a similar question here, however I fail to adapt the provided solutions to my case.
I want to have a jointplot with kind=hex while removing the marginal plot of the x-axis as it contains no information. In the linked question the suggestion is to use JointGrid directly, however Seaborn then seems to to be unable to draw the hexbin plot.
joint_kws = dict(gridsize=70)
g = sns.jointplot(data=all_data, x="Minute of Hour", y="Frequency", kind="hex", joint_kws=joint_kws)
plt.ylim([49.9, 50.1])
plt.xlim([0, 60])
g.ax_joint.axvline(x=30,ymin=49, ymax=51)
plt.show()
plt.close()
How to remove the margin plot over the x-axis?
Why is the vertical line not drawn?
Also is there a way to exchange the right margin to a plot which more clearly resembles the density?
edit: Here is a sample of the dataset (33kB). Read it with pd.read_pickle("./data.pickle")
I've been fiddling with an analog problem (using a scatterplot instead of the hexbin). In the end, the solution to your first point is awkwardly simple. Just add this line :
g.ax_marg_x.remove()
Regarding your second point, I've no clue as to why no line is plotted. But a workaround seems to be to use vlines instead :
g.ax_joint.vlines(x=30, ymin=49, ymax=51)
Concerning your last point, I'm afraid I haven't understood it. If you mean increasing/reducing the margin between the subplots, you can use the space argument stated in the doc.

why are my matplotlib plots come out as png's only and not as Matlab figures?

i use a piece of code to produce figures as i run in Pycharm, e.g.:
t=np.arange(1,5)
s=2*t+3
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',title='About as simple as it gets, folks')
ax.grid()
plt.show()
they come out as png's - how can i display them as figs (as in Matlab) - whats wrong?
In Pycharm, go to Preferences > Tools > Python Scientific, and disable Show plots in tool window (see screenshot attached). You may need to restart Pycharm after. Plots should appear in a separate, interactive pop-up window next time you plot with Matplotlib (that is, more similar to Matlab). If it doesn't work, check your matplotlib backend with matplotlib.get_backend(), and try different ones with matplotlib.use('<backend_name>'). You can access backend lists like this:
matplotlib.rcsetup.interactive_bk, matplotlib.rcsetup.non_interactive_bk,
matplotlib.rcsetup.all_backends.

Statsmodels Vector Autoregession - Plots Overalapping

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.

Accessing backend specific functionality with Julia Plots

Plots is simple and powerful but sometimes I would like to have a little bit more control over individual elements of the plot to fine-tune its appearance.
Is it possible to update the plot object of the backend directly?
E.g., for the default pyplot backend, I tried
using Plots
p = plot(sin)
p.o[:axes][1][:xaxis][:set_ticks_position]("top")
but the plot does not change. Calling p.o[:show]() afterwards does not help, either.
In other words: Is there a way to use the PyPlot interface for a plot that was initially created with Plots?
Edit:
The changes to the PyPlot object become visible (also in the gui) when saving the figure:
using Plots
using PyPlot
p = Plots.plot(sin, top_margin=1cm)
gui() # not needed when using the REPL
gca()[:xaxis][:set_ticks_position]("top")
PyPlot.savefig("test.png")
Here, I used p.o[:axes][1] == gca(). One has to set top_margin=1cm because the plot area is not adjusted automatically (for my actual fine-tuning, this doesn't matter).
This also works for subsequent updates as long as only the PyPlot interface is used. E.g., after the following commands, the plot will have a red right border in addition to labels at the top:
gca()[:spines]["right"][:set_color]("red")
PyPlot.savefig("test.png")
However, when a Plots command like plot!(xlabel="foo") is used, all previous changes made with PyPlot are overwritten (which is not suprising).
The remaining question is how to update the gui interactively without having to call PyPlot.savefig explicitly.
No - the plot is a Plots object, not a PyPlot object. In your specific example you can do plot(sin, xmirror = true).
I'm trying to do the same but didn't find a solution to update an existing plot. But here is a partial answer: you can query information from the PyPlot axes object
julia> Plots.plot(sin, 1:4)
julia> Plots.PyPlot.plt[:xlim]()
(1.0,4.0)
julia> Plots.plot(sin, 20:24)
julia> ax = Plots.PyPlot.plt[:xlim]()
(20.0,24.0)
and it gets updated.

How do I set the resize-behaviour of subplots?

I'm working with several subplots in a window. Just after plotting it looks great, problem is when I rezise the windows manually. The subplots do not adapt correctly to the new size of the window. How can I fix that?
Window after plotting, looking great:
window after resizing it manually, looks bad:
EDIT:
A simply demo-Code:
from matplotlib.pyplot import *
figure(figsize=(24,6))
subplot(131)
ylabel("test")
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")
subplot(132)
ylabel("test")
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")
subplot(133)
ylabel("test")
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")
tight_layout()
show()
As you see, the plot looks good after plotting it. But when you start to shrink the plot horizontally, the space between plot and plot gets smaller and smaller. And at the end, the ticklabels are on others plots, because there's no space for them. I need to know how to set that the entire plot gets smaller, leving space for the labels.
Maybe not exactly the answer of my question, but it solves my problem:
After creating the figure, you connect the resize-event to an eventhandler:
cid = fig.canvas.mpl_connect('resize_event', onresize)
def onresize(event):
plt.tight_layout()
As Wicket said, I'm just calling tight_layout() again and again, but automatically.