how are you. I've all day trying to do the following: I'm designing a PID controller and depending on how much data I want to analize then I show only one plot or four plots in the same figure (this last multiple plot is made with subplot).
All the calculation and plots are made in one function with options in function dependind on what I want to see, I have several buttons and text inputs in the application that I'm coding and right know the PID design and the simple or multiple plots are working; my current program is that the axes handle is being assigned to local variables inside the function and of course this variables are distroyed when the program exits the function.
A more detailed explanation of the problem is the following: if I select multiple plot and then I want one plot then I don't know how to delete the four axes to clean the figure without closing it, so when I want to activate the simple plot then the program only erases one of the four plots and prints the simple plot over the remaining three while the remaining three subplots remains visible. Following is the piece of code that is giving me problems:
grafico=sistem_graf;
disp(grafico);
if(isdef('aa')==%T);
scf(aa);
delete(get("current_axes"));
disp("aa");
end
if(isdef('bb')==%T);
scf(bb);
delete(get("current_axes"));
disp("bb");
end
if(isdef('cc')==%T);
scf(cc);
delete(get("current_axes"));
disp("cc");
end
if(isdef('dd')==%T);
scf(dd);
delete(get("current_axes"));
disp("dd");
end
select ventana
case 0
tipoG=0;
plot2d(T,graffta);
xtitle('Ω(S)/R(S): Respuesta al escalón (Sistema original)','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds = [1/3 0 2/3 1];
case 1
tipoG=1;
// delete(get("current_axes"));
plot(T,[graffta;sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Ω(S)/R(S): Respuesta al escalón','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds=[1/3 0 2/3 1];
case 2
tipoG=2;
//delete(get("current_axes"));
subplot(421);
plot(T,[graffta;sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Ω(S)/R(S): Respuesta al escalón','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds=[1/3,0,1/3,1/2]
subplot(422);
plot2d(T,(kt*kp/(j*la))*(1/(afta*bfta))*(1+(1/(afta-bfta))*(bfta*exp(-afta*T)-afta*exp(-bfta*T))));
xtitle('Ω(t)','t [s]','Vel [rpm]');
bb=get("current_axes");
bb.axes_bounds=[2/3,0,1/3,1/2];
subplot(423);
plot2d(T,(kt/(j*la))*(1/(afta*bfta))*((1/(afta-bfta))*(bfta*afta*exp(-bfta*T)-afta*bfta*exp(-afta*T))));
xtitle('Variación de velocidad angular dΩ/dt','t [s]','Ac. [rpm/s^2]');
cc=get("current_axes");
cc.axes_bounds=[1/3,1/2,1/3,1/2];
subplot(424);
plot(T,[sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Controladores','t [s]','Vel [rpm]');
legend(['C/control P';'C/Control PI';'C/Control PID']);
dd=get("current_axes");
dd.axes_bounds=[2/3,1/2,1/3,1/2];
end
the four if(isdef("variable_name")) never doesn't works because when the program enters the functions those variables doesn't exists yet, those are created after the plots with subplot.
If the graphics properties are displayed from the Edit menú of the figure then all the children axes are shown but if I try to get the axes with for example
a=get(sistem_graf.Axes(1))
then a console display is shown saying that the property "Axes" doesn't exist so I don't know how to solve the problem.
Please help!
Thanks in advance.
Update 03/06/2021:
I found a way to have the behavior I need an is working at 90%, it only fails when I go from detailed plot to simple plot (is erasing one graphic) but backwards it works fine, I changed all the code before "Select ventana" to the following
if(imps(1) ~= -1)
if(size(imps) == 1)
sca(imps(1));
disp(imps(1));
delete(get("current_axes"));
elseif(size(imps) > 1)
sca(imps(1));
delete(get("current_axes"));
sca(imps(2));
delete(get("current_axes"));
sca(imps(3));
delete(get("current_axes"));
sca(imps(4));
delete(get("current_axes"));
end
end
and filled an array whose content is all the axes ploted last time that the function was called, that array is returned from the function to a global variable and finally that global variable is feed to the function in the next call and assigned to the local variable "imps".
I think is some kind of crazy but apparently scilab doesn't have incorpotated the static variable aproach.
If I have understood your problem, the small example below should help you:
function fun(nb)
ch = gcf().Children;
delete(ch(ch.type=="Axes"))
t = linspace(0,1,100);
if nb==1
plot(t,t)
else
for i=1:nb
subplot(2,2,i)
plot(t,t.^i)
end
end
endfunction
clf
uicontrol("style","pushbutton","units","normalized","Position",[0.1 0.1 0.4 0.1],"string","one plot","callback","fun(1)");
uicontrol("style","pushbutton","units","normalized","Position",[0.5 0.1 0.4 0.1],"string","four plots","callback","fun(4)");
I think that the mechanism you were missing is the following:
ch = gcf().Children;
delete(ch(ch.type=="Axes"))
i.e. something that allows to delete the plot axes and keep the uicontrols alive. The above construct uses logical indexing. Here ch.type=="Axes" is a vector of boolean values with components i egal to true when the type field of corresponding component ch(i) is Axes. Then, ch(ch.type=="Axes") is the subset of components of ch such that the boolean index has value true. Hence, at last, the subset of components of ch which are of Axes type.
Related
I am trying to set up a series of vertical axis spans to symbolize different switching positions at different times. For example, in the figure below, switching position 1 (green) happens quite a few times, alternating between other positions.
I plot these spans running a for loop in a list of tuples, each containing the initial and final indexes of each interval to plot the axvspan.
def plotShades(timestamp, intervals, colour):
for i in range(len(intervals)):
md.plt.axvspan(timestamp[intervals[i][0]], timestamp[intervals[i][1]], alpha=0.5, color=colour, label="interval")
This function is then called upon another one, that plots the shades for each different switching position:
def plotAllOutcomes(timestamp, switches):
#switches is a list of 7 arrays indicating when the switcher is at each one of the 7 positions. If the array has a 1 value, the switcher is there. 0 otherwise.
colors = ['#d73027', '#fc8d59', '#fee08b', '#ffffbf', '#d9ef8b', '#91cf60', '#1a9850']
intervals = []
for i in range(len(switches)):
intervals.append(getIntervals(switches[i], timestamp))
plotShades(timestamp, intervals[i], colors[i])
md.plt.legend()
Doing so with the code snippets I've put here (not the best code, I know - I'm fairly new in Python!) the legend ends up having one item for each interval, and that's pretty awful. This is how it looks:
I'd like to get a legend with only 7 items, each for a single color in my plot of axvspans. How can I proceed to do so? I've searched quite extensively but haven't managed to find this situation being asked before. Thank you in advance for any help!!
A small trick you can apply using the fact that labels starting with "_" are ignored:
plt.axvspan( ... , label = "_"*i + "interval")
Thereby a label is only created for the case where i==0.
Ok, so I'm trying to use gscatter to plot 8 different points in a figure. These 8 points are all different and thus I want to give them different symbols. I know that gscatter will automatically assign them different colors, but I also want to be able to use the figure in black and white. I have written the following code:
lincol = {'k';'k';'k';'k';'k';'k';'k';'k'};
linsym = {'+';'o';'*';'.';'x';'s';'d';'^'};
limits = [-1 1 -1 1];
close all
for i = 1:3;
figure(i); hold on
gscatter(RfootXdistpertRel(:,i),RfootYdistpertRel(:,i),lincol,linsym);
legend('Pert1', 'Pert2', 'Pert3', 'Pert4', 'Pert5', 'Pert6', 'Pert7', 'Pert8')
hline(0);
vline(0);
axis(limits);
end
According to the matlab syntax, I should be able to specify color and marker symbol in this way (gscatter(x,y,col,sym)). The variables used are 8 by 1 vectors, just as the lincol and linsym. However, it gives me an error:
Error using plot
Color value must be a 3 or 4 element vector
Can anyone help? It's just such a silly problem to have.
Luc
It seems that you have some errors in your code. The syntax for gscatter should include at least 3 parameters : x, y and group. It seems that group is missing.
Furthermore the definition of color and sym may be wrong. Try col = 'kkkkkkkk'; instead of lincol = {'k';'k';'k';'k';'k';'k';'k';'k'};.
I hope this helps.
Regards.
Jonay
I have been trying to work with textplot in R and am unsure if my question is possible or not, I know that par() can't be used to place two textplots in one plot. I have been using a page and this code to try and figure things out.
My question is: Is it possible to have two textplots within the same plot?
For example, in the par(mfrow=c(1,1)) scenario below, plot 1 is a texplot of species length. Say I wanted to replicate that textplot twice in that plot. Is that possible?
based on this site:
http://svitsrv25.epfl.ch/R-doc/library/gplots/html/textplot.html
textplot(version)
data(iris)
par(mfrow=c(1,1))
info <- sapply( split(iris$Sepal.Length, iris$Species),
function(x) round(c(Mean=mean(x), SD=sd(x), N=gdata::nobs(x)),2) )
textplot( info, valign="top" )
title("Sepal Length by Species")
What I want to do is put a second textplot within that plot, underneath the original. For arguments sake, replicating that textplot twice in the plot.
Is this possible?
Thanks!
Maybe you've figured it out in the last four months but I thought I'd chip in an answer anyway.
The code provided is most of the way towards doing what you require already, you just have to provide some additional inputs to title() and/or par(). Namely specify that the title is to be above both of the plots by using title("your title", outer = TRUE) and you can further adjust the position of the title with an option in par(), use par(mfrow = c(2,1), oma = c(0,0,"top",0)). Hopefully this answers your question.
require('gplots')
data(iris)
info <- sapply(split(iris$Sepal.Length, iris$Species),
function(x) round(c(Mean = mean(x), SD = sd(x), N = gdata::nobs(x)),2))
## Replace top with a numerical value to control the position of the title with respect to the
## top of the page.
par(mfrow = c(2,1), oma = c(0,0, top ,0))
textplot(info, valign = "top")
textplot(info, valign = "top")
title("Sepal Length by Species", outer = TRUE)
I have a problem I solved in Excel but I am completely stuck in Matlab.
A weight measuring how much liquid I pump is refilled when its almost empty. Here is a picture
Now I want to see it in one motion, not like a jigsaw. Here is my solution, doing it manually in Excel:
Now to Matlab where this should be done automatically: I know how to index the row before and after I have the bumps, but I'm kind of stuck now. I do d=diff(x) ok and now I can replace the high peaks when the bumps occur (i=d(:,1)>0) with 0 so it never happened. And how do I translate it back? Somehow "undiff(x)"? im completely lost.
Here's an extract of my x:
2533,30
3540,00
3484,90
3430,00
3375,00
3320,20
3265,60
3210,60
3155,80
3101,20
3046,50
2991,70
2937,00
2882,50
2828,10
2773,80
2719,30
2664,90
2610,50
2556,10
2501,60
3508,00
3454,00
3399,70
3352,10
Like this?
temp = [0; diff(x)];
temp(temp < 0) = 0;
y = x - cumsum(temp);
y(temp > 0) = interp1(y, find(temp > 0) + 0.5);
plot(y);
hen using the default plot() function, matlab will automatically draw a line plot and connect each point in the data you are plotting.
Instead it seems like you just need to plot the points individually and unconnected. So if you are currently doing something like plot(x) or area(x) instead try plot(x,'o'). This will cause Matlab to plot the points individually without connecting them as lines.
If you'd like to change the marker type used to plot each point, use doc linespec to get more info.
I am running the following command to draw a few X,Y points in gnuplot:
plot "Output.tsv" using ($2+3):($3+3):1 with labels, "Output.tsv" using 2:3
Some of the data points are very close to each other and it makes the label unreadable. Is there a way to ask gnuplot to eliminate/reduce the overlap between labels?
I think you could consider 3 options:
1) make your graph huge and hope your labels do not overlap
2) plot the points as different series with each item having its own legend
3) use letters instead of labels, you can put a letter at each point using
plot "???" using 1:2
plot "" using 1:2:(stringcolumn(3) ne 'compare to' ? 'if equal' : 'if not equal' ) with labels
the stringcolumn function looks in column 3, compares the value to the string 'compareto' and if there is a match it puts 'if equal' at that location, otherwise 'if not equal'
Hence, I see something like Simulator in your graph, you could keep the green point and put an S with it/on it using
plot "" using 1:2:(stringcolumn(3) ne 'Simulator' ? 'S' : '' ) with labels
I hope this helps.