Create array of scatter plots with matplotlib [duplicate] - loops

This question already has answers here:
How to assign a plot to a variable and use the variable as the return value in a Python function
(3 answers)
Closed 5 months ago.
I'm currently creating scatters plot to represent the spin orientation for a ferromagnetic material, but this changes with time. What I'm trying to do is create the plots in a for loop. I can currently create each of the plots, but I don't know how to store and recall the plots I'm producing.
Ideally, I would like to recall a specific plot later in the document without having to save the plot as an image.
Is this at all possible?
I am currently using SageMath and Jupyter Notebooks.
This is currently my code to produce the scatter plots:
Beta = ellipsis_range(0.05,Ellipsis,0.8,step=0.05)
for i in range(len(Beta)):
B = Beta[i]
j = 0
if B<0.85:
plt.figure(figsize=((9,6)))
for y in range(1,N+1):
for x in range(1,N+1):
if state[j]==1:
plt.scatter(x,y,color="red",marker="^")
if state[j]==-1:
plt.scatter(x,y,color="blue",marker="v")
j = j+1
plt.title("Spin Distribution for Beta="+str(round(B,2)))
plt.show()
My eventual goal is to animate these plots.
Thank you in advance!

I have since managed to store my scatter plots as an array!
def spinplot(B,j,N):
plot = plt.figure(figsize=((9,6)))
for y in range(1,N+1):
for x in range(1,N+1):
if state[j]==1:
plt.scatter(x,y,color="red",marker="^")
if state[j]==-1:
plt.scatter(x,y,color="blue",marker="v")
j = j+1
plt.title("Spin Distribution for Beta="+str(round(B,2)))
return plot
In the original for-loop, I replaced the plotting code with:
j = 0
if B<0.85:
plots[k] = spinplot(B,j,N)
plt.close()
k = k+1
I am now trying to animate them. Which I will ideally save as a gif.

Related

Matlab: Plot array such that each value has random shape and a color map

In Matlab:
How do I modify plot(x,y,'o'), where x=1:10 and y=ones(1,10), such that each point in the plot will have a random shape?
And how can I give it colors chosen from a scheme where the value at x=1 is the darkest blue, and x=10 is red (namely some sort of heat map)?
Can this be done without using loops? Perhaps I should replace "plot" with a different function for this purpose (like "scatter"? I don't know...)? The reason is that I am plotting this inside another loop, which is already very long, so I am interested in keeping the running-time short.
Thanks!
First, the plain code:
x = 1:20;
nx = numel(x);
y = ones(1, nx);
% Color map
cm = [linspace(0, 1, nx).' zeros(nx, 1) linspace(1, 0, nx).'];
% Possible markers
m = 'o+*.xsd^vph<>';
nm = numel(m);
figure(1);
hold on;
for k = 1:nx
plot(x(k), y(k), ...
'MarkerSize', 12, ...
'Marker', m(ceil(nm * (rand()))), ...
'MarkerFaceColor', cm(k, :), ...
'MarkerEdgeColor', cm(k, :) ...
);
end
hold off;
And, the output:
Most of this can be found in the MATLAB help for the plot command, at the Specify Line Width, Marker Size, and Marker Color section. Colormaps are simply n x 3 matrices with RGB values ranging from 0 to 1. So, I interpreted the darkest blue as [0 0 1], whereas plain red is [1 0 0]. Now, you just need a linear "interpolation" between those two for n values. Shuffling the marker type is done by simple rand. (One could generate some rand vector with size n beforehand, of course.) I'm not totally sure, if one can put all of these in one single plot command, but I'm highly sceptical. Thus, using a loop was the easiest way right now.

R Programming: 3D array plots

I am trying to do up a 3D array plot in R.
I already have an array built up and defined with the corresponding z-values
e.g. CVHSP500 = array(0,c((nHSP500-N),N))
So now I am trying to do a 3D array plot with it. I decided to go with persp3d(CVHSP500,col = "lightblue",) and have obtained a rather decent plot.
3D Image
So there are obviously some issues with this plot.
1) The coordinates are not defined correctly.
Reading up online on the usage of persp3D, and other R programming functions/packages like slice3D, they all require x, y and z to be separate list.
I don't understand how to match the values of x and y to the respective z, and since persp3D works perfectly without me having to do that, I decided to use persp3D.
But I will need to insert coordinates for it, but I have no idea how to.
2) Any advice how do I color the plots for different ranges of z?
The ones online all seem to have to refer to individual x, y and z lists and some form of advanced modification which I can't really understand. This light blue color looks okay but it would be good for different ranges of z as well though.
Thanks for the help. Much appreciated.
To transform a 2D array representing z for each (x,y) into 3 vectors x, y and z, you can do this:
CVHSP500 = array(0,c((nHSP500-N),N))
x <- rep(1:(nHSP500-N),N)
y <- rep(1:N,(nHSP500-N))
z <- CVHSP500
dim(z) <- (nHSP500-N)*N

if loops and Arrays

Im trying to plot some data from an experiment but the timer starts a bit before the start of my variable. I have tried to create a new list of times starting from 0 but when I do so my loop generates more values for my array then the length of the original array which makes it impossible to plot.
Aplate = np.loadtxt('Plates Angular poston_2.txt')
t1 = []
for i in Aplate:
t = Aplate[:,0]
for j in t:
if j < 27.4150:
x = j -3.01
t1.append(x)
else:
break
y = Aplate[:,1]
plt.plot(t1, y)
plt.show()
If you are simply accounting for a constant delay in the start of data recording, why not just do
plt.plot( t-delay ,y )
where delay is 3.01? ( see here: https://stackoverflow.com/a/4918586/4916534 )
Also, if you want the columns to be the same length, why not populate y and t in the same loop? You are currently asking python to give you x values for all times < 27.4150, but to give you y values for all times.

Storing multiple powers of matrices in matlab [duplicate]

This question already has answers here:
How to generate the first twenty powers of x?
(4 answers)
Closed 7 years ago.
I have 1000 matrices with dimensions 2x2.
What I now need to do is to get 30 consecutive powers of those matrices (A^2, A^3... ...A^30) and store them all.
I found a topic that suggested using bsxfun:
Vectorizing the creation of a matrix of successive powers
However, bsxfun does not work with cell arrays ("Error using bsxfun
Operands must be numeric arrays").
What can I do?
PS. A secondary question: once I have them, I want to plot 4 graphs (each corresponding to 1 of the elements of the 2x2 matrices) with 30 positions (x-axis) which will show confidence bands (16th and 84th percentile).
EDIT: Someone linked to a question that was similar to the one that I linked. From what I can understand, the question there is about a vector, not array of matrices.
Assuming your array A is 2-by-2-by-1000, here are two loops to make things work:
A = rand(2,2,1000);
K = 30;
%%
N = size(A,3);
APower = zeros(2,2,N,K);
APower(:,:,:,1) = A;
for i = 1:N
for k = 2:K
APower(:,:,i,k) = A(:,:,i)*APower(:,:,i,k-1);
%// Alternatively you could use:
%APower(:,:,i,k) = A(:,:,i)^k;
end
end
You need to replicate the matrix 30 times to do this using cellfun. For example,
a = repmat(A{1},1,30);% Select one of your A matrices
b = num2cell(1:30);
x = cellfun(#(a,b) a^b,a,b,'UniformOutput',false)
Since you need to run cellfun for each element of A another way is to use arrayfun as below.
a = A{1};
b = 1:30;
x = arrayfun(#(b) a^b,b,'UniformOutput',false)

How to extract values from the loop and plot them in matlab?

This is my simple code:
for i=-20:20;
s=[-1 0 0];
e=[1 0 0];
r=[i 5 0];
b=e-r;
a=s-r;
w=cross(a,b);
y=dot(w,w);
z=dot(a,b);
u=norm(a);
v=norm(b);
k=dot(u,v);
g=1;
q=(w/y)*(u+v)*(1-z/k);
V=g/4*pi*q
end
But even such simple i can not figure out how to plot the results (only Z components of the vector V). Please help?
1) Create a figure f=figure();
2) Put hold on; command such that you draw the points in the same figure without overwriting the previous plot commands;
3) run the loop and use a plot of your choice. such as plot(i,z,'o'); here you draw a circle
Cheers
TL

Resources