I have two sin signals with the same frequency, and I plot a lissajou figure using them:
Now I want to calculate the phase difference between them. To do that, I have to know the values of a and b. How can I extract the value from the figure? By this I mean the exact coordinates of the largest x-position and the x-position where the curve crosses the zero line.
As example:
import numpy as np
import matplotlib as plt
t=np.arange(0,1.0,1.0/8000)
U=np.sin(2*np.pi*100*t)
I=np.sin(2*np.pi*100*t+45)
plt.plot(U,I)
plt.grid(True)
plt.show()
Related
I have a plot with several curves that looks like this:
These curves start from the top right corner and finish around the point (0.86, 0.5).
I want to focus attention on the end point. If I zoom on this region, it is still not very easy to distinguish the different lines because they overlap several times.
My idea is then to add a gradient of opacity so that the curves would be transparent at their start point and then, the opacity would increasing as we get closer to the end point.
How would you do that with matplotlib?
Currently, I just basically do for the three curves:
plt.plot( r, l )
with r, l being two arrays.
You could always break down your x and y arrays into smaller arrays that you plot separately. This would give you the opportunity to modify alpha for each segment.
See example below:
import numpy as np
import matplotlib.pyplot as plt
N_samp=1000
x=np.arange(N_samp)
y=np.sin(2*np.pi*x/N_samp)
step=10
[plt.plot(x[step*i:step*(i+1)],y[step*i:step*(i+1)],alpha=np.min([0.1+0.01*i,1]),color='tab:blue',lw=1) for i in range(int(N_samp/step))]
plt.show()
Is there any way to hold all the plots together (every plot is a point) in the following code:
import numpy as np
import matplotlib.pyplot as plt
g=10.7247;
x0=0;
z0=30;
v0=100*0.4889;
theta=np.pi/5;
vx=v0*np.cos(theta);
vz=v0*np.sin(theta);
for t in np.arange(0,10,0.1):
x=x0+vx*t;
z=z0+vz*t-1/2*g*t**2;
plt.scatter(x,z)
plt.xlim(0,300)
plt.ylim(0,100)
plt.pause(0.01)
if z<0:
break;
plt.show()
I used the plt.plot because I want the points to present one after the other but without any hold feature it returns a moving point. I want it to be like an animation. Every point should appear after 0.01s.
Here is exactly what I want: https://youtu.be/CK7Zq0kGiLY
First time user so apologies for any mistakes.
I have some code (pasted below) which is used to analyse and gain values/graphs from a simulation I have run.
This results in the following image:
I would therefore now like to plot a line graph on top of this according to the values of the colour map corresponding to r = 0 on the y-axis at every point on the x - axis with each respective value on the colour map. However, I'm completely lost on where to even begin with this. I've tried looking into KDE and other similar things, but I realise I'm not sure how to take numerical values which were used to generate the colour map.
from openpmd_viewer import OpenPMDTimeSeries
from openpmd_viewer.addons import LpaDiagnostics
import numpy as np
from scipy.constants import c, e, m_e
import matplotlib.pyplot as plt
from matplotlib import gridspec
# Replace the string below, to point to your data
ts = OpenPMDTimeSeries(r"/Users/bentorrance/diags/hdf5/")
ts_2d = LpaDiagnostics(r"/Users/bentorrance/diags/hdf5/")
plt.figure(1)
Ez = ts.get_field(iteration=5750, field='E', coord='z', plot=True, cmap='inferno')
plt.title(r'Electric Field Density $E_{z}$')
plt.show()
When plotting the estimated density function of my data using sns.kdeplot(), the algorithm extrapolates outside of the boundaries of the data, meaning that it draws the plot for values smaller than 0 or greater than 1, which is particularly annoying when dealing with probabilities. Example:
import numpy as np
import seaborn as sns
data = np.random.random(100)
sns.kdeplot(data)
How to fix that so that the actual plot remains within [0,1] in the x-direction without simply calling plt.xlim((0,1))?
I plot a couple of lines in log scale with a huge amount of points. I plot them in black using different line styles/markers. I use "markevery" property to decrease amount of markers. X-values change at even intervals.
The issue I have is that markers distributed unevenly - less of them near 0, and more near the right end of each line.
Is there are any way to get around this issue without nitpicking x-values, so that they will be "evenly" distributed on log-scale?
You can give the index of points you want to plot. In logscale these points should be non-uniformly distributed. You can try logspace to achieve it.
import pylab as plt
import numpy as np
x=np.arange(1,1e5)
# Normal plot
#plt.plot(x,x,'o-')
# Log plot
idx=np.logspace(0,np.log10(len(x)),10).astype('int')-1
plt.plot(x[idx],x[idx],'o-')
plt.xscale('log')
plt.yscale('log')
plt.show()
generates: