Issue
Suppose you have the following data:
x1_Length = 100
x1 = np.linspace(10,100,num=x1_Length)
y1 = np.linspace(0, 1.3, num=x1_Length)
x2_Length = 200
x2 = np.linspace(50, 200, num=x2_Length)
y2 = np.exp(np.linspace(0, 1, num=x2_Length))
If you wanted to plot this, it would be straightforward:
fig, ax = plt.subplots()
ax.plot(x1,y1, ".g")
ax.plot(x2,y2, "--r")
Which gives an expected result, EVEN THOUGH THE ARRAYS THAT WERE PLOTTED ARE OF DIFFERENT LENGTHS AND DO NOT SHARE THE x-data.
NOW, suppose you wanted to plot this same data in an intensity plot. This causes some problems:
it is my understanding that an intensity plot (for example
imshow
) plots 2D arrays and as such have no direct concept of an x-axis scale or y-axis scale. Data is presented based on its index in the array. Here, this would be a problem because the first elements of the two arrays DO NOT correspond to the same x-value.all the rows/columns need to be of the same length in a 2D array. This is the minor of the two problems, as i could always figure out the longest dataset and pad all shorter ones with, for example, NaNs, like so:
dataset = [x1_data, x2_data] max_Length = np.max([array[0,:].shape[0] for array in dataset ]) data_array = np.nan * np.ones((2*len(dataset), max_Length)) for i, spectrum_array2D in enumerate(dataset): length = spectrum_array2D[0,:].shape[0] firstRowOfInsertion = 2*i data_array[[firstRowOfInsertion, firstRowOfInsertion+1], :length] = spectrum_array2D
I don't see any technological limitation why one should not be able to plot several arrays of different data into an intensity plot much like plt.plot()
can. Ideally, what I need is to tell the intensity plot that it can expect, in this example, 2 horizontal slices of data, and then supply the x-data (plotted againt the x-axis), the y-data (plotted against the colorbar) for each index of the slice into which this plot should go. Do you know of a way to achieve this, or any package capable of this?
EDIT: Ultimately, I want a plot that can look like this, which was adopted from @wtw suggestion below (the data in this plot is random, I need it to be my y1 and y2 arrays):
Solution
Following discussions, I think what is wanted is a line plotted with x
points and colour values, possibly with a different y
value each time.
A slight modification on the answer here (just to pass plotting variables through): Color line by third variable - Python
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
def plot_colourline(x,y,c, **xargs):
c = cm.viridis((c-np.min(c))/(np.max(c)-np.min(c)))
ax = plt.gca()
for i in np.arange(len(x)-1):
ax.plot([x[i],x[i+1]], [y[i],y[i+1]], c=c[i], **xargs)
return
# First data set
x1 = np.arange(0, 10, 1)
y1 = np.zeros(len(x1)) # use 0 as the y value for all points
c1 = np.random.uniform(size=len(x1))
# Second dataset, different number of points, x values and y value
x2 = np.arange(8, 12, 0.1)
y2 = np.ones(len(x2)) # Use 1 as the y value for all points
c2 = np.random.uniform(size=len(x2))
fig, ax = plt.subplots(1)
plot_colourline(x1, y1, c1, linewidth=5)
plot_colourline(x2, y2, c2, linewidth=5, alpha=0.5)
Answered By - wtw
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.