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,
Related
Out of the box seaborn does a very good job to plot a 2D KDE or jointplot. However it is not returning anything like a function that I can evaluate to numerically read the values of the estimated density.
How can I evaluate numerically the density that sns.kdeplot or jointplot has put in the plot?
Just for completeness. I see something interesting in the scipy docs, stats.gaussian_kde but I am getting very clunky density plots,
which for some reason because of missing extent are really off compared to the scatter plot. So I would like to stay away from the scipy kde, at least until I figure how to make it work why pyplot is so much more "not smart" as seaborn is.
Anyhow, the evaluate method of the scipy.stats.gaussian_kde does its job.
I also faced this issue in jointplot() method. I opened a file distribution.py on this path anaconda3/lib/python3.7/site-packages/seaborn/. Then I added these lines in _bivariate_kdeplot() function:
print("xx=",xx[50])
print("yy=",yy[:,50])
print("z=",z[50])
This prints out 100 values of x,y and z arrays of 50 index. Where "z" is the density and "xx" and "yy" are the values adjusted according to the bandwidth, cut and clip, in a meshgrid form distributed according to grid size, that were given by the user. This gave me some idea about the actual values of the 2D kde plot.
If you print out entire array of each variable then you will get 100 x 100 values of each.
I am writing a code that allows me to obtain the linear regression of some measures. I have used different codes but with all of them I get strange result. Instead of being a line with a constant slope, the line I get is first horizontal and between the penultimate point and the last point the slope decreases.
The code I am using is:
import matplotlib.pyplot as plt
import numpy as np
x0=[0.00000001,0.000001,0.0001,0.01]
y0=[0.9974209723854539,0.9945196648709005,0.9914759279447916,0.9852556749265332]
x=np.array(x0)
y=np.array(y0)
m,b=np.polyfit(x,y,1)
print(m,b)
plt.scatter(x,y)
plt.plot(x,m*x+b,color='green')
plt.xscale('log')
d=['linear fit '+str(round(m,4))+'*x+'+str(round(b,4)),'real measure']
plt.legend(d,loc="upper right",borderaxespad=0.1,title="")
And I get the following graph:
Phyton plot
Which is very different from what I should get, which I have been able to draw in Origin:
Origin plot
I have tried various linear fit methods but with all of them I get this form which is wrong.
Hopefully you can help me find the error.
Thank you very much.
I have a program that generates around n = 100 000 segments, that I decided to store inside an array of shape (n,2,2) (where a segment is coded by the two points making its extremities). My plot is in 2D.
I've been trying to plot those segments with for n = 10 000 (that is way lower than I want) and it took around an hour to run and to appear on my screen. I do not have a really powerful computer (RAM: 8gb ; and a core i5 that is neither really powerful nor recent) but I hope that I can make things faster. The method I used was to go through my (n,2,2)-NumPy-array and plot a line between each pair of two points using matplotlib.
What I want:
1) Being able to draw those segments in static at first
2) Making things interactive (I want to see the projection of the set of those segments one axis line and then rotate my set to see how the projections changes)
EDIT: so I almost found out a way: plotting with pyqtgraph.
If I say "almost" that's because this is super fast when I plot every segment in one line, using the following code:
plotWidget = pg.plot(title="Test")
X = []
Y = []
for u in A:
X.append(u[0,0])
X.append(u[1,0])
Y.append(u[0,1])
Y.append(u[1,1])
plotWidget.plot(X,Y, pen='g')
Where A is a numpy array of shape (n,2,2). When I lunch it with n= 10 000 it takes less than one second to plot. However, it plots everything in one line and I want to plot it segment by segment.
So I tried the following code:
for u in A:
plotWidget.plot([u[0,0],u[1,0]],[u[0,1],u[1,1]],pen = 'g')
But after an hour, it didn't show up... I think it's because the call of "plot" is heavy (that's also maybe the problem with what I did with matplotlib).
I will try to use Vispy to see whether it works better for my case.
EDIT2: I discovered the collection in matplotlib. I used "Linecolletion" and now can plot really fast using matplotlib!
However, I would like to do the same with pyqtgraph. How to do so?
I have a fortran program that simulates some kind of radiation in the air and the signal obtained in each of the antennas at the ground. I am reading the output data with Python3, and one of the output .dat files holds information about the antennas, and it is organized in columns (Position along X-axis, Position along Y-axis, Signal detected, etc).
Supose that I have ~ 1000000 antennas and I have already storaged the position and signal of every one of them in the lists column_x ,column_y,signal and my objective is to reproduce a plot like the following:
How can I do that?
I tried to make a matrix with shape (len(column_x),len(column_y)), insert the values of the list signal in each place of the antenna and plot that matrix with plt.pcolor but I had a lot of issues. There must be an easiest way.
What you intend to do is exactly what matplotlib.pyplot.scatter can do:
import matplotlib.pyplot as plt
plt.scatter(column_x, column_y, c=signal)
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.