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.
Related
I was wondering whether it is possible to create a 3D vector Field PLot from a 4D numpy array.
The array is in the shape (10,10,10,3) which is basically the three vector components at each respective grid point. Grid spacing is 1.
There is a matplotlib function ax.quiver() (https://matplotlib.org/stable/gallery/mplot3d/quiver3d.html) that I would like to use. It can be used for irregular grid, which is why it takes 6 meshgrids as input (x,y,z,u,v,w) for the grid and the vector components respectively.
I know how to create the xyz meshgrids, but I am not sure how to extract the vector components uvw from my 4d array.
Help would be appreciated!
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 have a 3D data cube and I am trying to make a plot of the first axis at a specific value of the other two axes. The goal is to make a velocity plot at given coordinates in the sky.
I have tried to create an 1D array from the 3D array by putting in my values for the last two axes. This is what I have tried
achan=50
dchan = 200
lmcdata[:][achan][dchan] #this array has three axes, vchan, achan, dchan.
I am expecting an array of size 120 as there are 120 velocity channels that make up the vchan axis. When trying the code above I keep getting an array of size 655 which is the number of entries for the dchan axis.
Python slicing works from left to right. In this case, lmcdata[:] is returning the whole lmcdata list. So, lmcdata[:][achan][dchan] is equivalent to just lmcdata[achan][dchan].
For higher level indexing and slicing tasks like this, I highly recommend the numpy package. You will be able to slice lmcdata as expected after turning it into a numpy array: lmcdata = np.asarray(lmcdata).
This question already has answers here:
Why does pyplot.contour() require Z to be a 2D array?
(5 answers)
Closed 5 years ago.
I plot a contour plot which indicates the seperating hyperplane of a SVC estimator in a 2D axes using the following code.
X,y= make_circles(n_samples=50,factor=.1,noise=.1)
x_fit=np.linspace(-1.5,1.5,10)
y_fit=np.linspace(-1.5,1.5,10)
Y,XX=np.meshgrid(x_fit,y_fit)
xy=np.vstack([XX.ravel(),Y.ravel()]).T
P=clf.decision_function(xy).reshape(XX.shape)
plt.contour(XX,Y,P,colors="k",levels=[-1,0,1],alpha=0.5,linestyles=["--","-","--"])
Question
Based on this question and the answer of Ilya V. Schurov there is still one issue for me. I understand, that X and Y provides the x and y values and Z provides the "depth" for each xy coordiante and thus has to be 2 dimensional. Further, the X and Y values of the plt.contour() function can be either 1D or 2D (if 1D the meshgrid gets computed internally).
BUT what is the benefit/ reason for X and Y to be 2D?
Because actually the "second dimension" of X and Y can not be plotted on a 2D axes. So has it some "algorithmic performance" reasons for X and Y to be 2D or what is the reason?
Contour plot is not designed for just plotting hyperplanes for classfier. It represents a 3-D surface with a 2-D format; or it plots elevations of a 2-D area. Therefore, plt.contour() has to somehow understand/know elevations covering the whole area. One way, or the current way, is to provide a set of elevations for a set of points covering the 2-D area. And the more you provide, the better/finer the final contour plot is. When providing a 1-D x and y, it represents a line rather than an area, which cannot be used to interpolated a 2-D area.
Another way to plot hyperplanes is to calculate the exact planes yourself. Then you can plot hyperplanes with a 1-D linespace. But I don't think this will be easier than using plt.contour() since plt.contour() did the hard calculation by simulating with interpolation for you.
Edit: How Z works with X and Y in plt.contour()?
It takes some assumption for Z works with X and Y.
If X and Y is 2-D, a value in Z is the depth for a point specified by corresponding (same location by index) values in X and Y.
If X and Y is 1-D, it will be convert to a meshgrid first, as you can see in the source code. Then the rest will work the same way as explained above.
So for your case specifically, using x_fit and y_fit can give you the same result because plt.contour() makes the meshgrid for you. As long as you understand the mechanism, either way is fine. The only thing I would say is if you end up making the meshgrid for calculating P anyway, why not using the meshgrid to avoid assumption/ambiguity?
I have several N-dimensional arrays of different shapes and want to combine them into a new (N+1)-dimensional array, where the new axis has a length corresponding to the number of initial N-d arrays.
This answer is sufficient if the original arrays are all the same shape; however, it does not work if they have different shapes.
I don't really want to reshape the arrays to a congruent size and fill with empty elements due to the subsequent analysis I need to perform on the final array.
Specifically, I have four 4D arrays. One of the things I want to do with the resulting 5D array is plot parts of the four arrays on the same matplotlib figure. Obviously I could plot each one separately, however soon I will have more than four 4D arrays and am looking for a dynamic solution.
While I was writing this, Sven gave the same answer in the comments...
Put the arrays in a python list in the following manner:
5d_list = []
5d_list.append(4D_array_1)
5d_list.append(4D_array_2)
...
Then you can unpack them:
for 4d_array in 5d_list:
#plot 4d array on figure