Related
I have data in form of a 3D array, with "intensities" at every point. Depending on the intensity, I want to plot the point with a higher alpha. There are a lot of low-value outliers, so color coding (with scalar floats) won't work since they eclipse the real data.
What I have tried:
#this generates a 3D array with higher values around the center
a = np.array([0,1,2,3,4,5,4,3,2,1])
aa = np.outer(a,a)
aaa = np.einsum("ij,jk,jl",aa,aa,aa)
x_,y_,z_,v_ = [],[],[],[]
from matplotlib.colors import to_rgb,to_rgba
for x in range(aaa.shape[0]):
for y in range(aaa.shape[1]):
for z in range(aaa.shape[2]):
x_.append(x)
y_.append(y)
z_.append(z)
v_.append(aaa[x,y,z])
r,g,b = to_rgb("blue")
color = np.array([[r,g,b,a] for a in v_])
fig = plt.figure()
ax = fig.add_subplot(projection = '3d')
ax.scatter(x_,y_,z_,c =color)
plt.show()
the scatterplot documentation says that color can be a 2D array of RGBA, which I do pass. Hoever when I try to run the code, I get the following error:
ValueError: 'c' argument has 4000 elements, which is inconsistent with 'x' and 'y' with size 1000.
I just found my own answer.
The "A 2D array in which the rows are RGB or RGBA." statement in the documentation was a bit confusing - one needs to convert the RGBA rows to RGBA objects first, so that list comprehension should read:
color = np.array([to_rgba([r,g,b,a]) for a in v_])
I want to create this graph 1 in python using matplotlib. I created a list called generation that is initialized with values from 0 to 200. I created a list variable consisting of 38 lists. Each list consists of 200 float numbers. I tried to plot the data but I have the error:
ValueError: x and y must have same first dimension, but have shapes (200,) and (38,)
My code:
generation = []
for i in range(200):
generation.append(i)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("A test graph")
for i in range(len(listt)):
#generation is a list with 200 values
#listt is a list with 38 lists
#list1 is a list with 200 values
plt.plot(generation ,[list1[i] for list1 in listt],label = 'id %s'%i)
plt.legend()
plt.show()
The final graph I want to look like the one below:
Each line in this graph 1 corresponds to a different input value. For each input, the algorithm runs 100 generations. The graph shows how the results of the algorithm evolve over 100 generations.
You're almost there! You only need to use listt[i] instead of [list1[i] for list1 in listt].
So, the code should look like this:
import matplotlib.pyplot as plt
#random listt
listt = [[i for j in range(200)] for i in range(38)]
generation = []
for i in range(200):
generation.append(i)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("A test graph")
for i in range(len(listt)):
plt.plot(generation ,listt[i],label = 'id %s'%i) #<--- change here
plt.legend()
plt.show()
And it returns this graph:
Of course, it won't be exactly as yours since I randomly generated listt.
I have a python directory, the keys of which stores the percentage range: 0_5, 5_10, 10_15,....80_85, 85_90, 90_95, 95_100. The value for each key is the count number in the whole data. I want to use matplotlib to plot a histogram to see its distribution and there will be 20 bins and each bin should be labeled with its percentage range and there will be a little spacing between each bin so that they are seperated.
I've tried this code and it gives me histogram that has 20 bins. But it's not what I need.
commutes = pd.Series(counts)
commutes.plot.hist(grid = False, bins = 20, rwidth = 0.8, color = 'tomato', edgecolor='gray', label = 'Type 1')
Also, I tried this and it shows the error: ValueError: weights should have the same shape as x
pylab.hist(ratio.keys(), weights = ratio.values(), bins=range(20))
This is how I created the directory. The variable counts stores a list of 700 percentage values.
for i in range(0,100,5):
start = i
end = i+5
key = str(start)+"_"+str(end)
number = 0
for count in counts:
if((count >= start) and (count < end)):
number = number + 1
ratio[key] = number
ratio.keys() and ratio.values() are of type dict_keys and dict_values. I'm guessing that matplotlib is trying to apply np.array() or np.asarray() to them, which does not work as intended, i.e. it gets converted to array(dict_keys([...]), dtype=object) rather than an array of numbers.
A simple fix is to convert the dictionary keys and values to lists first.
pylab.hist(list(ratio.keys()), weights=list(ratio.values()), bins=range(len(ratio)))
As I am new to python programming. I have a problem in the for loop with index error. I have gone through the suggestions that you have given me. My problem is that in the for loop...
I didn't get any error with this code below...
for i in range(0,1):
But I have obtained an error if the limit exceeds for example (0,3)
for i in range(0,3):
The error is
IndexError: index 1 is out of bounds for axis 0 with size 1
I have tried to clear out this error and I am not sure that why this error occurs in the for loop if the limits exceed 1.
This is my code:
m=['paketone4000.dump.xlsx','paketone8000.dump.xlsx','paketone12000.dump.xlsx']
fig_name=['j4000','e8000','e12000']
fig=plt.figure(figsize=(6,6)) ##to obtain figure and dimensions of graph
for i in range(0,3):
#ax=fig.add_subplot(111,projection='3d') ## to have a broad view of figure
ax = fig.add_axes([0,0,1,1], projection='3d')
#plot planes
p = Rectangle((0,-0.7), 4.5,1.4, color="lightgrey", alpha=0.2) #plots the background frame
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=0, zdir="z")
j=pd.read_excel(m[i]) ##to read the excel file format
X=j['x'] ## to import the variable on to axes from data set
Y=j['y']
Z=j['z']
#ax.scatter(X,Y,Z,c='g', marker='o') ## to specify the color and shape of point(marker) of the frame
a=j['x']##import centre of mass from excel file format
b=j['y']
c=j['z']
q1=j['q1'], ##attaining quaternons from excel file format. (comma(,) transformed series to tuple)
q2=j['q2'],
q3=j['q3'],
q4=j['q4'],
m,n,o,p=np.array([q1,q2,q3,q4]) ## assigning quaternions to variables had converted tuple to float
Rot_Mat=QtoR(m,n,o,p)
#cuboid initialising parameters
center = [a[0], b[0], c[0]] ##centre of the body
length = 0.3 ##defining length, breadth, height
width = 0.4
height = 0.1
side = np.zeros((8,3)) ###This numpy vector will be used to store the position of the sides
#rotate the axes and update
for angle in range(0, 360):
ax.view_init(90, angle)
cuboid(center, (length, width, height)) #to execute the defined cuboid
plt.savefig(fig_name[i])
plt.clf()
print("\nq1=",m,"q2=",n,"q3=",o,"q4=",p)
print('\nRotation Matrix=',Rot_Mat)
print ("\nCenter = \n",center)
My expected result is that I want to remove the error that was obtained and I am interested in to know why that error occurred when end limit is greater than one.
You're using the name m for two different variables in your code. At the top of the file you use it to create a list of filenames, which you read in the loop. But later in the loop, you reassign it with this line:
m,n,o,p=np.array([q1,q2,q3,q4])
That causes the error when you try to read later files, as the new m value doesn't contain what the code expects (and may not be the expected size).
You should use two different variable names. This kind of issue suggest that it might be a good idea to use longer, more descriptive variable name, as you are a lot less likely to have this kind of random namespace collision with names like filenames and first_quaternion (or whatever).
I'd also suggest using range(len(m)) so that if you change the size of the list at some future time, you won't need to remember to also change the hard-coded range size.
An image for this code execution. How about you try replacing
for i in range(0, 5):
with
for i in range(len(m)):
EDIT:
Does this work?
m=['paketone4000.dump.xlsx','paketone8000.dump.xlsx','paketone12000.dump.xlsx']
fig_name=['j4000','e8000','e12000']
fig=plt.figure(figsize=(6,6)) ##to obtain figure and dimensions of graph
for index, i in enumerate(m):
#ax=fig.add_subplot(111,projection='3d') ## to have a broad view of figure
ax = fig.add_axes([0,0,1,1], projection='3d')
#plot planes
p = Rectangle((0,-0.7), 4.5,1.4, color="lightgrey", alpha=0.2) #plots the background frame
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=0, zdir="z")
j=pd.read_excel(i) ##to read the excel file format
X=j['x'] ## to import the variable on to axes from data set
Y=j['y']
Z=j['z']
#ax.scatter(X,Y,Z,c='g', marker='o') ## to specify the color and shape of point(marker) of the frame
a=j['x']##import centre of mass from excel file format
b=j['y']
c=j['z']
q1=j['q1'], ##attaining quaternons from excel file format. (comma(,) transformed series to tuple)
q2=j['q2'],
q3=j['q3'],
q4=j['q4'],
m2,n,o,p=np.array([q1,q2,q3,q4]) ## assigning quaternions to variables had converted tuple to float
Rot_Mat=QtoR(m2,n,o,p)
#cuboid initialising parameters
center = [a[0], b[0], c[0]] ##centre of the body
length = 0.3 ##defining length, breadth, height
width = 0.4
height = 0.1
side = np.zeros((8,3)) ###This numpy vector will be used to store the position of the sides
#rotate the axes and update
for angle in range(0, 360):
ax.view_init(90, angle)
cuboid(center, (length, width, height)) #to execute the defined cuboid
amount_of_files_to_rename=index
new_names = [i*1000 for i in range(4*amount_of_files_to_rename)[::4]]
for i in new_names:
plt.savefig('packetone {}.jpg'.format(i))
#plt.savefig(fig_name[b])
#plt.clf()
print("\nq1=",m2,"q2=",n,"q3=",o,"q4=",p)
print('\nRotation Matrix=',Rot_Mat)
print ("\nCenter = \n",center)
I'd like to make a plot where each point it has its x&y value and it also has a third value expressing the color density at that point. Applying my python code in mathematica I am able to do it using the following code, but now I want to do it only using python(preferably using matlibplot).
def printMath2DTableMethod():
print('{', end="")
for i in range(0, lines, 1):
print('{', end="")
for j in range(0, columns, 1):
f = int(columns * rearrange_.rearrangeMethod(i) + rearrange_.rearrangeMethod(j))
print('%d' % size[f], end = '')
if (j < columns - 1):
print(',', end='')
if (i < lines - 1):
print('},')
else:
print('}}')
The plotting should look something similar to the images of these two questions
How can I make a scatter plot colored by density in matplotlib?
How to plot a density map in python?
it should have a colorbar at the side and the points with the biggest density should be on the top of the other points(if they overlap).
The data that this method produces I append it to some file and it looks like:
1,2,4,5,6,2,6 x256 columns in total
3,2,4,5,1,6,4
4,2,5,6,1,7,5
x256 rows in total
The plotting can be made by using the code directly or by reading the data from the file, but what I don't know is how to assign values to x(which is the i at the 1st for loop at the code above), to y(which is the j at the 2nd for loop at the code above) and especially to the 3rd argument, the one which will show the color density(which is the size[f] at the code above) since it is depended on i and j of the for loops.
I have been trying to research and solve it myself all these days, but not much success, so any help would be highly appreciated. Thanks in advance :)
Here are examples for both plots you linked
import matplotlib.pyplot as plt
import scipy as sp
# scatterplot as link 1
Data = sp.randn(1000,3)
plt.scatter(Data[:,0],Data[:,1],c=Data[:,2],cmap='magma')
plt.colorbar()
# density matrix as link 2
Nbins = 50
M = sp.zeros((Nbins+1,Nbins+1))
xinds = sp.digitize(Data[:,0],sp.linspace(-3,3,Nbins)) # chose limits accordingly
yinds = sp.digitize(Data[:,1],sp.linspace(-3,3,Nbins))
# to account for the highest density drawn over the others
sort_inds = sp.argsort(Data[:,2])[::-1]
Data = Data[sort_inds,:]
xinds = xinds[sort_inds]
yinds = yinds[sort_inds]
for i in range(Data.shape[0]):
M[xinds[i],yinds[i]] = Data[i,2]
plt.matshow(M,cmap='magma',
extent=(Data[:,0].min(),Data[:,0].max(),Data[:,1].max(),Data[:,1].min()),
aspect='equal')
plt.colorbar()