Converting an array with values in log scale to a regular array - python

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?

Related

How to create large 3D array in Python/Numpy from coordinates and values

I would like to create 3D array of points, where I have list of three coordinates (x,y,z) and value at this coordinate. I have 6308 points of measurement.
For reference, I pasted the data in the pastebin. The purpose is to create 3D Numpy array and calculate 3D Fourier transform.
I would like to arrange this data into 3D array. I tried to use xarray module for this - but the xarray.DataArray needs numpy.ndarray as its input.
I tried to create meshgrid and put data value into coordinates but I had MemoryError.
Finally, I tried to make test random array:
out = np.zeros(shape=(6308,6308,6308))
but I also had the error:
MemoryError: Unable to allocate 1.83 TiB for an array with shape (6308, 6308, 6308) and data type float64
And at this point I don't have any idea how to arrange the data into 3D array for 3D Fourier transform calculation. Is it even possible?

Python 3D Vector Field Plot from 4D numpy array

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!

python 2d contourf plot using 3d np array data

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.

matplotlib.pyplot.hist returns a histogram where all bins have the same value when I have varying data

I am trying to create a histogram in python using matplotlib.pyplot.hist.
I have an array of data that varies, however when put my code into python the histogram is returned with values in all bins equal to each other, or equal to zero which is not correct.
The histogram should look the the line graph above it with bins roughly the same height and in the same shape as the graph above.
The line graph above the histogram is there to illustrate what my data looks like and to show that my data does vary.
My data array is called spectrumnoise and is just a function I have created against an array x
x=np.arange[0.1,20.1,0.1]
The code I am using to create the histogram and the line graph above it is
import matplotlib.pylot as mpl
mpl.plot(x,spectrumnoise)
mpl.hist(spectrumnoise,bins=50,histtype='step')
mpl.show()
I have also tried using
mpl.hist((x,spectrumnoise),bins=50,histtype=step)
I have also changed the number of bins countless times to see if that helps an normalising the histogram function but nothing works.
Image of the output of the code can be seen here
The problem is that spectrumnoise is a list of arrays, not a numpy.ndarray. When you hand hist a list of arrays as its first argument, it treats each element as a separate dataset to plot. All the bins have the same height because each 'dataset' in the list has only one value in it!
From the hist docstring:
Multiple data can be provided via x as a list of datasets
of potentially different length ([x0, x1, ...]), or as
a 2-D ndarray in which each column is a dataset.
Try converting spectrumnoise to a 1D array:
pp.hist(np.vstack(spectrumnoise),50)
As an aside, looking at your code there's absolutely no reason to convert your data to lists in the first place. What you ought to do is operate directly on slices in your array, e.g.:
data[20:40] += y1

Numpy Index values for each element in 3D array

I have a 3D array created using the numpy mgrid command so that each element has a certain value and the indexes retain the spatial information. For example, if one summed over the z-axis (3rd dimension) then the the resultant 2D array could be used in matplotlib with the function imshow() to obtain an image with different binned pixel values.
My question is: How can I obtain the index values for each element in this grid (a,b,c)?
I need to use the index values to calculate the relative angle of each point to the origin of the grid. (eg. theta=sin-1(sqrt(x^2+y^2)/sqrt(x^2+y^2+z^2))
Maybe this can be translated to another 3D grid where each element is the array [a,b,c]?
I'm not exactly clear on your meaning, but if you are looking for 3d arrays that contain the indices x, y, and z, then the following may suit your needs; assume your data is held in a 3D array called "abc":
import numpy as nm
x,y,z = nm.mgrid[[slice(dm) for dm in abc.shape]]

Categories