I want to plot an array ndarray by matplotlib which is defined as
dataset = np.ndarray(shape=(len(image_files), image_size, image_size),
dtype=np.float32)
My question is what is the syntax of plt.plot() will be in case of plotting arrays like this?
Thank You
If your array is three dimensional you cannot directly plot it in two dimensions.
In your case it seems that you store several images along the first axis of the array. So for an array
dataset = np.ndarray(shape=(len(image_files), image_size, image_size),
dtype=np.float32)
You can plot the ith image out of it via
plt.imshow(dataset[i,:,:])
If this does not answer the question, you'd need to be more specific about what exactly you want to plot.
Related
I have two matrices: m x m and m x 1.
The 2D matrix's data is dependent on the 1D matrix, so I want to show this case in a one plot.
Earlier, to visualize the 2D matrix I used a heatmap and also now I think about using it.
Both matrices have the same label ticks, so I got an idea to double the 1D matrix and place them next to the 2D matrix's labels to let them share the same label ticks:
Is it possible to achieve such a plot^ in Python, especially in seaborn or matplotlib?
Or maybe there is another, better way to visualize such a data?
For now, I see here one issue - a lack of a legend for 1D matrices, but I have no idea where and how it should be placed.
I am trying to implement this paper. I have to try to interpolate the latent code of an autoencoder, as mentioned in the paper. The latent code is the encoded input of an autoencoder. The shape of the latent code (for two samples) is (2, 64, 64, 128).
This is what I have done:
image1 = sel_train_encodings[0]
image2 = sel_train_encodings[1]
x = image1[:,0,0]
x_new = image2[:,0,0]
new_array = interp1d(x, image1, axis=0, fill_value='extrapolate', kind='linear')(x_new)
I basically took the encodings of two images and tried to interpolate( with extrapolation for some points as all points don't lie in the same range) and then did interpolation over one of the axes. But the results I later obtain with these interpolated values are not so good, am I doing something wrong/how else to do it?
According to one of the given answers, I also tried to do 2D interpolation in the following way:
image1 = sel_train_encodings[0]
image2 = sel_train_encodings[1]
new_array = griddata((x,z),y,(x_new, z_new), method='cubic', fill_value='extrapolate')
But this resulted in the error:
ValueError: shape mismatch: objects cannot be broadcast to a single shape
Scipy has a couple of 2D interpolation routines, depending on the spacing of the (x, y):
If your data is on a regular grid, try scipy.interpolate.RectBivariateSpline(). This is probably most applicable to images.
If your data is collected on a grid with uneven rectangular intervals, you want to use scipy.interpolate.interp2d().
If all bets are off and the (x, y) are scattered without any clear grid, try scipy.interpolate.griddata()
I have a problem here.
My data is a 3d shape of np array
(256, 256, 1) = (x coordinate, y coordinate, pressure value)
Now I would like to draw a contour plot using this np array.
But the problem is that the shape of my data does not fit into plt.contourf
Any idea on how to preprocess my data before feeding it to contourf?
Since you have a singular value for each position [M,N], you can simply squeeze out that dimension and have it represented by a matrix of shape [M,N].
data = data.squeeze(2)
plt.contourf(data)
The squeezed and original array contain the exact same data, but are just represented slightly differently.
I want to visualize such 4D data in the 2D plane. Is there any way to do that?
You can use principal component analysis to reduce the dimensions. Sklearn has an easy to use package. PCA uses eigen vectors to determine the most 'important' directions in the higher dimensional space and projects the values into the lower dimensional space. Although keep in mind the generated vectors do lose some information from the higher dimensional input.
You can use pairs plot. Look at this link: Visualizing Data with Pairs Plots in Python.
Depending on the data type, you can plot them on a 2d field with the following dimensions:
Dim 1-2: X and Y axes
Dim 3: plotted point size
Dim 4: plotted point color gradient
Or if you do it with a 3D software, you can plot in 3D, with all the point plotted with color gradient. While rotating the field, you can have vision on all dimensions.
I am trying to plot a histogram of elements of a downloaded .py file. The array had a shape (200, 3649, 13). So I had reshaped it to a 2D array by preforming the function
samples = chain[:,-2000:,:].reshape((-1,13))
I hence plotted the histogram by doing
plt.hist(samples[:,3])
The graph gave me values in log scale, but I need to plot it in a linear scale. How do I proceed?