I would like to find the intersection of two graphs. It took me 674 points to plot the first graph and only 14 points to plot the second graph.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_csv("test1.csv",,skiprows=range(9),names=['A', 'B', 'C','D'])
df2 = pd.read_csv("test2.csv",skiprows=range(1),names=['X','Y'])
x1 = df['A'].tolist()
x1 = np.array(x1)
y1 = df['D'].tolist()
y1 = np.array(y1)
x2 = df2['X'].tolist()
x2 = np.array(x2)
y2 = df2['Y'].tolist()
y2 = np.array(y2)
idx = np.argwhere(np.diff(np.sign(y1 - y2))).flatten()
fig, ax = plt.subplots()
ax.plot(x1, y1, 'blue')
ax.plot(x2, y2, 'red')
plt.show()
However, I am getting this error from the code above due to the different sizes of numpy. Any ways I can solve this?
operands could not be broadcast together with shapes (674,) (14,)
You should compute interpolations of both curves with scipy.interpolate.interp1d, then you can calculate indeces of intersection points with the method you used.
I assume you have two curves with x1, x2, y1 and y2 coordinates, with different lengths and x axis limits:
x1 = np.linspace(-2, 12, 674)
x2 = np.linspace(0, 8, 14)
y1 = np.sin(x1)
y2 = np.cos(x2) + 1
So, you have to compute interpolation functions:
f1 = interp1d(x1, y1, kind = 'linear')
f2 = interp1d(x2, y2, kind = 'linear')
Then, you need to evaluate new curves on a common x axis, so new curves will have the same length:
xx = np.linspace(max(x1[0], x2[0]), min(x1[-1], x2[-1]), 1000)
y1_interp = f1(xx)
y2_interp = f2(xx)
Finally, you can compute indices of interpolation points as you already did:
idx = np.argwhere(np.diff(np.sign(y1_interp - y2_interp))).flatten()
Complete Code
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
x1 = np.linspace(-2, 12, 674)
x2 = np.linspace(0, 8, 14)
y1 = np.sin(x1)
y2 = np.cos(x2) + 1
f1 = interp1d(x1, y1, kind = 'linear')
f2 = interp1d(x2, y2, kind = 'linear')
xx = np.linspace(max(x1[0], x2[0]), min(x1[-1], x2[-1]), 1000)
y1_interp = f1(xx)
y2_interp = f2(xx)
idx = np.argwhere(np.diff(np.sign(y1_interp - y2_interp))).flatten()
fig, ax = plt.subplots()
ax.plot(x1, y1, 'blue', label = 'y1')
ax.plot(x2, y2, 'red', label = 'y2')
for index in idx:
ax.plot(xx[index], y1_interp[index], marker = 'o', markerfacecolor = 'black', markeredgecolor = 'black')
plt.show()
Plot
Related
I have two arrays for x-values and two corresponding arrays for y-values which I wish to plot.
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(-2,2,100)
x2 = np.linspace(0,4,100)
y1 = x1**2+1
y2 = (x2-1.5)**2
plt.plot(x1,y1)
plt.plot(x2,y2)
plt.show()
This produces the following plot.
But instead of this, I want to plot only the minima of these two curves, i.e. only the region of y1 where y1<y2 and only the region of y2 where y2<y1. Something like this.
Since x1 and x2 are different, I can't use np.minimum(). Is there an efficient way to do this with numpy and/or matplotlib?
I would like to have a general approach that also works when y1 and y2 are not determined from some function I know, but are taken from e.g. a dataset.
You could interpolate both functions onto a common x, and then take their minimum.
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(-2, 2, 100)
x2 = np.linspace(0, 4, 100)
y1 = x1 ** 2 + 1
y2 = (x2 - 1.5) ** 2
plt.plot(x1, y1, ls=':')
plt.plot(x2, y2, ls=':')
xc = np.sort(np.concatenate([x1, x2]))
y1c = np.interp(xc, x1, y1, left=y2.max(), right=y2.max())
y2c = np.interp(xc, x2, y2, left=y1.max(), right=y1.max())
plt.plot(xc, np.minimum(y1c, y2c), lw=10, alpha=0.4)
plt.show()
One solution is to cross compute the functions with the other xs and mask:
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(-2,2,100)
x2 = np.linspace(0,4,100)
y1 = x1**2+1
y1b = x2**2+1
y2 = (x2-1.5)**2
y2b = (x1-1.5)**2
plt.plot(x1, np.where(y1<y2b, y1, np.nan))
plt.plot(x2, np.where(y2<y1b, y2, np.nan))
plt.show()
output:
I am trying to shade the area between two curves that I have plotted.
This is what I plotted.
Using the following code.
plt.scatter(z1,y1, s = 0.5, color = 'blue')
plt.scatter(z2,y2, s = 0.5, color = 'orange')
I tried using plt.fill_between() but for this to work I need to have the same data on the x_axis (would need to do something like plt.fill_between(x,y1,y2)).
Is there any other function that might help with this or am I just using fill_between wrong.
You can try with:
plt.fill(np.append(z1, z2[::-1]), np.append(y1, y2[::-1]), 'lightgrey')
For example:
import numpy as np
import matplotlib.pyplot as plt
x1 = np.array([1,2,3])
y1 = np.array([2,3,4])
x2 = np.array([2,3,4,5,6])
y2 = np.array([1,2,3,4,5])
# plt.plot(x1, y1, 'o')
# plt.plot(x2, y2, 'x')
plt.scatter(x1, y1, s = 0.5, color = 'blue')
plt.scatter(x2, y2, s = 0.5, color = 'orange')
plt.fill(np.append(x1, x2[::-1]), np.append(y1, y2[::-1]), 'lightgrey')
plt.show()
Try this code:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2 * np.pi * x)
y2 = 1.2 * np.sin(4 * np.pi * x)
fig, (ax1) = plt.subplots(1, sharex=True)
ax1.fill_between(x, 0, y1)
ax1.set_ylabel('between y1 and 0')
I want to plot multiple lines in a 3d axis plot.
I'm plotting the trajectory of a light ray, each line should be colored depending on the time of propagation for each path.
I want to plot them independently, since I saved them in a binary three structure in which any ray may follow two different trajectories.
I used Line3DCollection, but it doesn't seem to be the right way.
In few words,
import numpy as np
x = np.linspace(0,1,100)
y = x
z = x
t = np.linspace(0,1,100)
#here I need to plot these points coloring them in function of t
x1 = np.linspace(1,2,100)
y1 = x
z1 = x
t1 = np.linspace(1,2,100)
#I need to plot also this used the same colorbar used for former values
x2 = -np.linspace(1,2,100)
y2 = x
z2 = x
t2 = np.linspace(1,2,100)
#idem
having all the lines colored with the same colorbar scale, like this
plot.
I tried with the following but the output is not like I expected
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Line3DCollection
x = np.linspace(0,1,100)
y = x
z = x
t = np.linspace(0,1,100)
points = np.array([x,y,z]).transpose().reshape(-1,1,3)
segs = np.concatenate([points[:-1],points[1:]],axis=1)
lc = Line3DCollection(segs, cmap=plt.get_cmap('jet'))
lc.set_array(t)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.add_collection3d(lc)
x1 = np.linspace(1,2,100)
y1 = x1
z1 = x1
t1 = np.linspace(1,2,100)
points1 = np.array([x1,y1,z1]).transpose().reshape(-1,1,3)
segs1 = np.concatenate([points1[:-1],points1[1:]],axis=1)
lc = Line3DCollection(segs1, cmap=plt.get_cmap('jet'))
lc.set_array(t1)
lc.update_scalarmappable()
ax.add_collection3d(lc)
x2 = np.linspace(1,2,100)
y2 = -x2+2
z2 = -x2+2
t2 = np.linspace(1,2,100)
points2 = np.array([x2,y2,z2]).transpose().reshape(-1,1,3)
segs2 = np.concatenate([points2[:-1],points2[1:]],axis=1)
lc = Line3DCollection(segs2, cmap=plt.get_cmap('jet'))
lc.set_array(t1)
lc.update_scalarmappable()
ax.add_collection3d(lc)
ax.set_xlim(0, 2)
ax.set_ylim(0, 2)
ax.set_zlim(0, 2)
fig.colorbar(lc)
plt.show()
plotting this plot
Thanks in advance
I want to plot two figures in one image using matplotlib. Data which I want to plot is:
x1 = ['sale','pseudo','test_mode']
y1 = [2374064, 515, 13]
x2 = ['ready','void']
y2 = [2373078, 1514]
I want to plot the bar plot for both the figure in one image. I used the code given below:
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x1, y1)
ax1.set_title('Two plots')
ax2.plot(x2, y2)
but its giving error:
ValueError: could not convert string to float: PSEUDO
How I can plot them in one image using matplotlib?
Try this:
x1 = ['sale','pseudo','test_mode']
y1 = [23, 51, 13]
x2 = ['ready','void']
y2 = [78, 1514]
y = y1+y2
x = x1+x2
pos = np.arange(len(y))
plt.bar(pos,y)
ticks = plt.xticks(pos, x)
Separate figures in one image:
x1 = ['sale','pseudo','test_mode']
y1 = [23, 51, 13]
x2 = ['ready','void']
y2 = [78, 1514]
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
pos1 = np.arange(len(y1))
ax1.bar(pos1,y1)
plt.sca(ax1)
plt.xticks(pos1,x1)
pos2 = np.arange(len(y2))
ax2.bar(pos,y2)
plt.sca(ax2)
plt.xticks(pos2,x2)
The problem is that your x values are not numbers, but text. Instead, plot the y values and then change the name of the xticks (see this answer):
import matplotlib.pyplot as plt
x1 = ['sale','pseudo','test_mode']
y1 = [23, 51, 13]
x2 = ['ready','void']
y2 = [78, 1514]
f, axes = plt.subplots(1, 2, sharey=True)
for (x, y, ax) in zip((x1, x2), (y1, y2), axes):
ax.plot(y)
ax.set_xticks(range(len(x))) # make sure there is only 1 tick per value
ax.set_xticklabels(x)
plt.show()
This produces:
For a bar graph, switch out ax.plot(y) with ax.bar(range(len(x)), y). This will produce the following:
I am trying to plot a figure with 4 subplots and 2 colorbars. Here is my code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import rcParams
rcParams["savefig.dpi"] = 100
rcParams["font.size"] = 18
x1 = np.linspace(100, 1000, 10)
y1 = np.linspace(10, 17, 10)
z1 = np.linspace(4, 18, 10)
t1 = np.linspace(-0.3, 0.4, 10)
fig, axes = plt.subplots(2, 2, sharey = True, figsize = (10, 10))
a0 = axes[0][0].scatter(x1, y1, s = 40, c = z1, marker = 'o')
cbar1 = fig.colorbar(a0)
axes[0][0].set_ylabel('y1')
axes[0][0].set_xlabel('x1')
axes[0][0].xaxis.set_major_locator(MaxNLocator(4))
a1 = axes[0][1].scatter(t1, y1, s = 40, c = z1, marker = 'o')
axes[0][1].xaxis.set_major_locator(MaxNLocator(4))
axes[0][1].set_xlabel('t1')
cbar1.ax.set_ylabel('z1', rotation = 270)
x2 = np.linspace(450, 900, 20)
y2 = np.linspace(11, 12.5, 20)
z2 = np.linspace(12, 60, 20)
t2 = np.linspace(-0.3, 0.4, 20)
a0 = axes[1][0].scatter(x2, y2, c = z2, marker = 'o')
cbar2 = fig.colorbar(a0)
axes[1][0].set_ylabel('y2')
axes[1][0].set_xlabel('x2')
axes[1][0].xaxis.set_major_locator(MaxNLocator(4))
a1 = axes[1][1].scatter(t2, y2, c = z2, marker = 'o')
axes[1][0].xaxis.set_major_locator(MaxNLocator(4))
axes[1][1].set_xlabel('t2')
cbar2.ax.set_ylabel('z2', rotation = 270)
plt.show()
Here is the figure:
The thing I want to fix is:
the colorbars are at the far end to the right. I want 1 colorbar to be on the right of the first row and another colorbar to be on the right of the second row (basically, where it simply is).
How can I do this? Thanks!
You can enter another parameter to select on which axes to plot the colorbar.
Here is the change in your code:
cbar1 = fig.colorbar(a0, ax=axes[0][1])
cbar2 = fig.colorbar(a0, ax=axes[1][1])
Which produce this plot: