dynamically plot the optimization result in Matplotlib - python

I'm doing a numerical optimization, and I want to visualize the loss of the objective function at each iteration, which means I need to add one point when each iteration is done into the plot.
I thought this might be a matplotlib.animation job, but it seems that animation just updates the plot by interval period of time, this is not what I want.
After searching SO, I indeed find a tricky solution, but is there a better way?

To set the data of a scatter in-place, you need to do the following:
pc = ax.scatter( x, y, ...)
and modify:
px.set_offsets( xnew, ynew )
before invoking wither draw (slower) or the blit method you linked.

Related

Colors bounds for streamplot

I need to create a streamplot with a colorbar indicating the speed of a given line. The thing is, there's 2 points in my speed data with excessive value, totally rescaling the scale of the colorbar, and I can't really erase them.
I've searched way to prevent it without modifying the data (like with cmin, Vmin, Vmax...), but it seems streamplot doesn't have function for this...
So is there a way to add limits to the cmap in a streamplot ?

Plotting a changing function on one graph

I am implementing a machine learning algorithm that attempts to do a linear regression by implementing gradient descent. What I want to do is to plot the points, then plot the currently computed line; then, as the parameters change, I want the new function to then replace the old one, and so on until the iterations are done.
I define the x, and y arrays to fit the data:
x=np.array([170,187,166,157,157,166])
y=np.array([190,187,153,150,162,166])
(Oh, and I did import numpy as np
import matplotlib.pyplot as plt)
I then plot the points by:
plt.scatter(x,y)
When I have the current iteration of the regression line (the current slope(m) and y-int(b), I do this:
plt.plot(x, m*x+b)
It only plots the final version of the line rather than the intermediate lines. I want to make a kind of cool "animation" to show the computer "learning" the correct line. I would probably pause between each "guess," then erase the line and draw a new one.
I want to plot the line, erase that line, then plot a new line. Not sure how to implement that,

How can I work around overflow error in matplotlib?

I'm solving a set of coupled differential equations with odeint package from scipy.integrate.
For the integration time I have:
t=numpy.linspace(0,8e+9,5e+06)
where 5e+06 is the timestep.
I then plot the equations I have as such:
plt.xscale('symlog') #x axis logarithmic scale
plt.yscale('log',basey=2) #Y axis logarithmic scale
plt.gca().set_ylim(8, 100000) #Changing y axis ticks
ax = plt.gca()
ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
plt.title("Example graph")
plt.xlabel("time (yr)")
plt.ylabel("quantity a")
plt.plot(t,a,"r-", label = 'Example graph')
plt.legend(loc='best')
where a is time dependent variable. (This is just one graph from many.)
However, the graphs look a bit jagged, rather than oscillatory and I obtain this error:
OverflowError: Exceeded cell block limit (set 'agg.path.chunksize' rcparam)
I'm not overly sure what this error means, I've looked at other answers but don't know how to implement the 'agg.path.chunksize'.
Also, the integration + plotting takes around 7 hours and that is with some CPU processing hacks, so I really do not want to implement anything that would increase the time.
How can I overcome this error?
I have attempted to reduce the timestep, however I obtain this error instead:
Excess work done on this call (perhaps wrong Dfun type).
Run with full_output = 1 to get quantitative information.
As the error message suggests, you may set the chunksize to a larger value.
plt.rcParams['agg.path.chunksize'] = 1000
However you may also critically reflect why this error occurs in the first place. It would only occur if you are trying to plot an unreasonably large amount of data on the graph. Meaning, if you try to plot 200000000 points, the renderer might have problems to keep them all in memory. But one should probably ask oneself, why is it necessary to plot so many points? A screen may display some 2000 points in lateral direction, a printed paper maybe 6000. Using more points does not make sense, generally speaking.
Now if the solution of your differential equations requires a large point density, it does not automatically mean that you need to plot them all.
E.g. one could just plot every 100th point,
plt.plot(x[::100], y[::100])
most probably without even affecting the visual plot appearance.

Trim data outside 3d plot in matplotlib

I have a set of PDF that I need to plot for a certain section of the PDF domain. However, when I plot my lines on a 3d plot I get tails for each PDF,
Is there a clean way to not plot the tails that happen outside my plot limits? I know I can change the data to NaNs to achieve the same effect but I want to do this in matplotlib. Here is my current workaround code,
`# trim the data
y = np.ones(PDF_x.shape)*PDF_x
y[y>95]= np.nan
y[y<75]= np.nan
# plot the data
fig = plt.figure()
ax = fig.gca(projection='3d')
for i in range(PDF_capacity.shape[1]):
ax.plot(life[i]*np.ones((PDF_x.shape)),y,PDF_capacity[:,i], label='parametric curve')
# set the axis limits
ax.set_ylim(75,95)
# add axis labels
ax.set_xlabel('charge cycles to failure point of 75% capacity')
ax.set_ylabel('capacity at 100 charge cycles')
ax.set_zlabel('probability')`
After trimming I can make the following plot,
Masking the data with nan in the way you're doing it is a good and practical solution.
Since matplotlib 3D plots are projections into 2D space, it would be hard to implement automatic clipping. While I do think it would be possible, I'm not convinced that it's worth the effort. First, because you would need to treat different kinds of plots differently, second, because at least in some cases it would probably turn out that masking the data is still the best choice. Now, doing a complex subclassing of the plotting objects just to do the same thing that can be manually done in one or two lines is probably overkill.
My clear recommendation would therefore be to use the solution you already have. Especially since it does not seem to have any drawbacks so far.

Timed / Artist Animations in MatPlotLib

I love Matplotlib but sometimes the lack of 'idiots guide' examples is infuriating.
Long story short, I have several large lists of XYZ positional data from simulated motion throw 3D space from multiple entities. I currently do this statically, i.e.
for entity in entities:
x=map(itemgetter(0),positionLog(entity))
y=map(itemgetter(1),positionLog(entity))
z=map(itemgetter(2),positionLog(entity))
ax.plot(x,y,z,label=nameLookup(entity))
plt.show()
What I'd like to do is to have these lists 'step' out, i.e where all the entities are at t(0), then add in the t(1) points and so on.
However, it's not clear in any of the examples I've found how to accomplish this. The examples that I see show how to do individual runs, i.e. for one entity, but I can't see how to do all (N) in lock-step.
Suggestions please? :D
So one way to do what I think you want is to make x, y, and z lists. add t(0) to the plot and show the plot. Next, append t(1) to you original x, y, z lists, update the plot with the new x, y, z coordinates, then refresh the plot (which is the old way of doing animations in matplotlib).
This example: http://matplotlib.sourceforge.net/examples/animation/basic_example.html
uses the built in animation function to generate an animation the new way, which is exactly what I think you want, just add your third coordinate.

Categories

Resources