Controlling the Axis of a NumPy Histogram - python

I have a sorted, 1D NumPy array which contains 156k integers that range from 263,168 to 24,064,000. I don't know how the items in the array are distributed so I am trying to plot the data as a histogram.
I've tried setting different values for the bins parameter but I still end up with a plot that doesn't allow me to see the distribution.
import numpy as np
from matplotlib import pyplot as plt
np_ary = # integer array of data
print(np_ary)
plt.hist(np_ary, bins='auto')
plt.show()

Related

Value Error :shape mismatch: objects cannot be broadcast to a single shape, in my code

I tried to animate my time dependent curve, by storing each frame containg the curve at a certain time step,but caught up with this reoccuring error,
Below mentioned is my code for the same:
import numpy as np
import matplotlib
import matplotlib.pyplot as plot
from matplotlib.animation import PillowWriter
fig=plt.figure()
l,=plt.plot([],[],'k-')
metadata=dict(title="Psi(x,t)",artist="Me")
writer=PillowWriter(fps=15,metadata=metadata)
xlist_t=[]
ylist_t=[]
with writer.saving(fig,"Psi(x,t).gif",100):
for n in n_values:
xlist_t.append(n)
ylist_t.append(PSI[n])
l.set_data(xlist_t,ylist_t)
writer.grab_frame()
PSI is a multi dimensional array whose each row corresponds to a particular value of n

Python matplotlib: how to let matrixplot have variable column widths

I have a simple need but cannot find its simple solution. I have a matrix to plot, but I wish the row/columns to have given widths.
Something looking like a blocked matrix where you tell block sizes.
Any workaround with the same visual result is accepted.
import matplotlib.pyplot as plt
import numpy as np
samplemat = np.random.rand(3,3)
widths = np.array([.7, .2, .1])
# Display matrix
plt.matshow(samplemat)
plt.show()
matshow or imshow work with equal sized cells. They hence cannot be used here. Instead you may use pcolor or pcolormesh. This would require to supply the coordinates of the cell edges.
Hence you first need to calculate those from the given width. Assuming you want them to start at 0, you may just sum them up.
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(43)
samplemat = np.random.rand(3,3)
widths = np.array([.7, .2, .1])
coords = np.cumsum(np.append([0], widths))
X,Y = np.meshgrid(coords,coords)
# Display matrix
plt.pcolormesh(X,Y,samplemat)
plt.show()

matplotlib turn an array into a parametric plot

Suppose I have the following script:
import numpy as np
import matplotlib.pyplot as plt
A = np.array([[1,1,1,0],[0,0,1,0],[0,1,0,0],[0,0,0,0]])
How can I plot just the values of A that are equal to 1, leaving the 0's blank? Basically I'm looking to plot just those points, and not as a pcolormesh or something similar.
If you change the values to non-integer values they will not appear in your array.
x(x == -1) = NaN;
plot(x)

Plotting random point on Function - Pandas

I want to graph a function 2D or 3D
for example a f(x) = sin(x)
Then randomly plot a certain amount of points
I am using IPython and I think this might be possible using Pandas
You can use np.random.uniform to generate a few random points along x-axis and calculate corresponding f(x) values.
import numpy as np
import matplotlib.pyplot as plt
# generate 20 points from uniform (-3,3)
x = np.random.uniform(-3, 3, size=20)
y = np.sin(x)
fig, ax = plt.subplots()
ax.scatter(x,y)
You should post example code so people can demonstrate it more easily.
(numpy.random.random(10)*x_scale)**2
Generate an array of random numbers between 0 and 1, scale as appropriate (so for (-10,0);
10*numpy.random.random(100) -10
then pass this to any function that can calculate the value of f(x) for each element of the array.
Use shape() if you need to play around with layout of the array.
If you want to use Pandas...
import pandas as pd
import matplotlib.pyplot as plt
x=linspace(0,8)
y=sin(x)
DF=pd.DataFrame({'x':x,'y':y})
plot values:
DF.plot(x='x',y='y')
make a random index:
RandIndex=randint(0,len(DF),size=20)
use it to select from original DF and plot:
DF.iloc[RandIndex].plot(x='x',y='y',kind='scatter',s=120,ax=plt.gca())

Numpy Array to Graph

I have a simple 2D Numpy array consisting of 0s and 1s. Is there a simple way to make a graph that will shade in corresponding coordinates?
For example if my array was [[1,0],[0,1]]
The plot would be a 2x2 square with the top left and bottom right shaded in
You can use matplotlib to plot a matrix for you.
Use the matshow command with an appropriate colourmap to produce the plot.
For example
import numpy as np
import matplotlib.pyplot as plt
x = np.array([[1,0],[0,1]])
plt.matshow(x, cmap='Blues')
plt.show()
would produce:

Categories