Multiple sliders in interactive octave plot - user-interface

I am trying to create a GUI where multiple variables could be modified using sliders. This is my example this far: I'm trying to alter the gradient of a linear function by summing two sliders.
I am completely new to the gui of octave and matlab, so I assume there is some fundamental error.
%%%%%% In file myplot.m %%%%%
function myplot
%% Create initial figure and spiral plot
figure;
axes ('position', [0.1, 0.3, 0.8, 0.6]);
global t;
t = linspace (0, 100, 101)
x = t;
y = t;
plot (x, y);
axis ([-100, 100, -100, 100]);
%% Add ui 'slider' element
hslider = uicontrol ( ...
'style', 'slider', ...
'Units', 'normalized', ...
'position', [0.1, 0.1, 0.8, 0.1], ...
'min', -100, ...
'max', 100, ...
'value', 0, ...
'callback', {#plotstuff} ...
);
%% Add ui 'slider' element
kslider = uicontrol ( ...
'style', 'slider', ...
'Units', 'normalized', ...
'position', [0.1, 0, 0.8, 0.1], ...
'min', -100, ...
'max', 100, ...
'value', 0, ...
'callback', {#plotstuff} ...
);
end
%% Callback function called by slider event
%% Also in file myplot.m (i.e. a subfunction)
function plotstuff (h, k, event)
global t;
n = get (h, 'value');
m = get (k, 'value');
x = t;
y = (n+m) * t ;
plot (x, y);
axis ([-100, 100, -100, 100]);
end
This is my error message:
㎫ >> error: operator *: nonconformant arguments (op1 is 0x0, op2 is 1x101)
error: called from
octave_test>plotstuff at line 43 column 5
error: operator *: nonconformant arguments (op1 is 0x0, op2 is 1x101)
error: called from
octave_test>plotstuff at line 43 column 5
This is the figure window:

function plotstuff (h, k, event)
This signature is wrong. First argument to the callback function is a handle to a slider object. Second argument is the event. There is no third argument. This is the correct form:
function plotstuff(h, event)
In this callback, h equals either hslider or kslider (the numbers returned by uicontrol).
You have multiple options here. You can make hslider and kslider global, as you did with t, and use them to get the slider values in the callback:
%% in myplot
global hslider kslider
...
%% in plotstuff
global hslider kslider
...
n = get (hslider, 'value');
m = get (kslider, 'value');
If you don't want to use global variables, you can pass the two handles as additional arguments to the callback:
%% in myplot
hslider = uicontrol(...)
kslider = uicontrol(...)
set(hslider, 'callback', {#plotstuff, hslider, kslider})
set(kslider, 'callback', {#plotstuff, hslider, kslider})
Then you can use these arguments in plotstuff:
function plotstuff (handle, event, hslider, kslider)
n = get(hslider, 'value');
m = get(kslider, 'value');

Related

Adding tick marks and line on barplot

I need help adding tick marks to my barplot. I would like an X-axis with tick marks in intervals of 100. I would also like to add a vertical red line at X=1350. How can I add that to the following code:
H <- c(1350, 1436) # Create the data for the chart, cont3.#
M <- c(
"Target
Attendance",
"Actual
Attendance"
)
c <-barplot(H, col =c("slategray3","dodgerblue4" ),
names.arg = M, horiz = TRUE,
family="Arial", border = NA,
xlim = range(0,1600),
axes = TRUE, las=1)
One can use the axis() and abline() functions to modify the base graphics:
barplot(H, col =c("slategray3","dodgerblue4" ),
names.arg = M, horiz = TRUE,
family="Arial", border = NA,
xlim = range(0,1600),
axes = FALSE, las=1)
axis(1, at=seq(0, 1500, 100))
abline(v=1350, col="red")
Add the "lwd=" option to the abline function to change the line's width.

reverse the color for heatmap colorbar [duplicate]

I would like to know how to simply reverse the color order of a given colormap in order to use it with plot_surface.
The standard colormaps also all have reversed versions. They have the same names with _r tacked on to the end. (Documentation here.)
The solution is pretty straightforward. Suppose you want to use the "autumn" colormap scheme. The standard version:
cmap = matplotlib.cm.autumn
To reverse the colormap color spectrum, use get_cmap() function and append '_r' to the colormap title like this:
cmap_reversed = matplotlib.cm.get_cmap('autumn_r')
In matplotlib a color map isn't a list, but it contains the list of its colors as colormap.colors. And the module matplotlib.colors provides a function ListedColormap() to generate a color map from a list. So you can reverse any color map by doing
colormap_r = ListedColormap(colormap.colors[::-1])
As of Matplotlib 2.0, there is a reversed() method for ListedColormap and LinearSegmentedColorMap objects, so you can just do
cmap_reversed = cmap.reversed()
Here is the documentation.
As a LinearSegmentedColormaps is based on a dictionary of red, green and blue, it's necessary to reverse each item:
import matplotlib.pyplot as plt
import matplotlib as mpl
def reverse_colourmap(cmap, name = 'my_cmap_r'):
"""
In:
cmap, name
Out:
my_cmap_r
Explanation:
t[0] goes from 0 to 1
row i: x y0 y1 -> t[0] t[1] t[2]
/
/
row i+1: x y0 y1 -> t[n] t[1] t[2]
so the inverse should do the same:
row i+1: x y1 y0 -> 1-t[0] t[2] t[1]
/
/
row i: x y1 y0 -> 1-t[n] t[2] t[1]
"""
reverse = []
k = []
for key in cmap._segmentdata:
k.append(key)
channel = cmap._segmentdata[key]
data = []
for t in channel:
data.append((1-t[0],t[2],t[1]))
reverse.append(sorted(data))
LinearL = dict(zip(k,reverse))
my_cmap_r = mpl.colors.LinearSegmentedColormap(name, LinearL)
return my_cmap_r
See that it works:
my_cmap
<matplotlib.colors.LinearSegmentedColormap at 0xd5a0518>
my_cmap_r = reverse_colourmap(my_cmap)
fig = plt.figure(figsize=(8, 2))
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
norm = mpl.colors.Normalize(vmin=0, vmax=1)
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap = my_cmap, norm=norm,orientation='horizontal')
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap = my_cmap_r, norm=norm, orientation='horizontal')
EDIT
I don't get the comment of user3445587. It works fine on the rainbow colormap:
cmap = mpl.cm.jet
cmap_r = reverse_colourmap(cmap)
fig = plt.figure(figsize=(8, 2))
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
norm = mpl.colors.Normalize(vmin=0, vmax=1)
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap = cmap, norm=norm,orientation='horizontal')
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap = cmap_r, norm=norm, orientation='horizontal')
But it especially works nice for custom declared colormaps, as there is not a default _r for custom declared colormaps. Following example taken from http://matplotlib.org/examples/pylab_examples/custom_cmap.html:
cdict1 = {'red': ((0.0, 0.0, 0.0),
(0.5, 0.0, 0.1),
(1.0, 1.0, 1.0)),
'green': ((0.0, 0.0, 0.0),
(1.0, 0.0, 0.0)),
'blue': ((0.0, 0.0, 1.0),
(0.5, 0.1, 0.0),
(1.0, 0.0, 0.0))
}
blue_red1 = mpl.colors.LinearSegmentedColormap('BlueRed1', cdict1)
blue_red1_r = reverse_colourmap(blue_red1)
fig = plt.figure(figsize=(8, 2))
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
norm = mpl.colors.Normalize(vmin=0, vmax=1)
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap = blue_red1, norm=norm,orientation='horizontal')
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap = blue_red1_r, norm=norm, orientation='horizontal')
There is no built-in way (yet) of reversing arbitrary colormaps, but one simple solution is to actually not modify the colorbar but to create an inverting Normalize object:
from matplotlib.colors import Normalize
class InvertedNormalize(Normalize):
def __call__(self, *args, **kwargs):
return 1 - super(InvertedNormalize, self).__call__(*args, **kwargs)
You can then use this with plot_surface and other Matplotlib plotting functions by doing e.g.
inverted_norm = InvertedNormalize(vmin=10, vmax=100)
ax.plot_surface(..., cmap=<your colormap>, norm=inverted_norm)
This will work with any Matplotlib colormap.
There are two types of LinearSegmentedColormaps. In some, the _segmentdata is given explicitly, e.g., for jet:
>>> cm.jet._segmentdata
{'blue': ((0.0, 0.5, 0.5), (0.11, 1, 1), (0.34, 1, 1), (0.65, 0, 0), (1, 0, 0)), 'red': ((0.0, 0, 0), (0.35, 0, 0), (0.66, 1, 1), (0.89, 1, 1), (1, 0.5, 0.5)), 'green': ((0.0, 0, 0), (0.125, 0, 0), (0.375, 1, 1), (0.64, 1, 1), (0.91, 0, 0), (1, 0, 0))}
For rainbow, _segmentdata is given as follows:
>>> cm.rainbow._segmentdata
{'blue': <function <lambda> at 0x7fac32ac2b70>, 'red': <function <lambda> at 0x7fac32ac7840>, 'green': <function <lambda> at 0x7fac32ac2d08>}
We can find the functions in the source of matplotlib, where they are given as
_rainbow_data = {
'red': gfunc[33], # 33: lambda x: np.abs(2 * x - 0.5),
'green': gfunc[13], # 13: lambda x: np.sin(x * np.pi),
'blue': gfunc[10], # 10: lambda x: np.cos(x * np.pi / 2)
}
Everything you want is already done in matplotlib, just call cm.revcmap, which reverses both types of segmentdata, so
cm.revcmap(cm.rainbow._segmentdata)
should do the job - you can simply create a new LinearSegmentData from that. In revcmap, the reversal of function based SegmentData is done with
def _reverser(f):
def freversed(x):
return f(1 - x)
return freversed
while the other lists are reversed as usual
valnew = [(1.0 - x, y1, y0) for x, y0, y1 in reversed(val)]
So actually the whole thing you want, is
def reverse_colourmap(cmap, name = 'my_cmap_r'):
return mpl.colors.LinearSegmentedColormap(name, cm.revcmap(cmap._segmentdata))

How to code a slider in Octave to have interactive plot?

my target is to have a plot that shows Stochastic oscillator on forex market, and in order to validate which parameter is the best one to setup it, I would use a slider to modify it and show updated result on plot.
I have my historical data, for a defined pair (let say AUDUSD) and after loading it, I calculate Stocastic oscillator:
function [stoch, fk, dk] = stochastic(n, k, d)
X=csvread("AUDUSD_2017.csv");
C=X(2:length(X),5);
L=X(2:length(X),4);
H=X(2:length(X),3);
O=X(2:length(X),2);
for m=n:length(C)-n
stoch(m)=((C(m)-min(L(m-n+1:m)))/(max(H(m-n+1:m))-min(L(m-n+1:m))))*100;
endfor
for m=n:length(C)-n
fk(m)=mean(stoch(m-d:m));
endfor
for m=n:length(C)-n
dk(m)=mean(fk(m-d:m));
endfor
endfunction
This is a picture of what I have when I plot stoch, fk and dk:
I would add 3 sliders to the figure in order to change, in a range, parameters as input, so i.e. to have a slider that changes first parameter "n" between 3 and 50, "k" between 2 and 20, and "d" between 2 and 20.
I would use UI package in octave, can someone address me to have a plot updated when I use sliders?
Francesco
Have a look at this demo which will give you an window like that which should answer all your questions:
The relevant parts for your specific questions are:
h.noise_slider = uicontrol ("style", "slider",
"units", "normalized",
"string", "slider",
"callback", #update_plot,
"value", 0.4,
"position", [0.05 0.25 0.35 0.06]);
....
noise = get (h.noise_slider, "value");
Be sure to use the Qt toolkit!
Andy pointed out in the comments that the example I linked to doesn't work on octave out of the box; this is because Octave doesn't like nested functions for certain things for the time being, so I've reproduced an 'octave version' below.
%%%%%% In file myplot.m %%%%%
function myplot
%% Create initial figure and spiral plot
figure; axes ('position', [0.1, 0.3, 0.8, 0.6]);
global t; t = linspace (0, 8*pi, 100);
x = t .* cos(t); y = t .* sin(t);
plot (x, y); axis ([-100, 100, -100, 100]);
%% Add ui 'slider' element
hslider = uicontrol ( ...
'style', 'slider', ...
'Units', 'normalized', ...
'position', [0.1, 0.1, 0.8, 0.1], ...
'min', 1, ...
'max', 50, ...
'value', 10, ...
'callback', {#plotstuff} ...
);
end
%% Callback function called by slider event
%% Also in file myplot.m (i.e. a subfunction)
function plotstuff (h, event)
global t;
n = get (h, 'value');
x = n * t .* cos(t); y = n * t .* sin(t);
plot (x, y); axis ([-100, 100, -100, 100]);
end

nvd3 - multiBarChart: how to log scale y axis

According the multiBarChart doc it should be possible to set yScale on multiBarChart as yScale should be inherited method.
I'd like to scale it to logarithmic scale. I got this working with lineChart.yScale(d3.scale.log()), but for multiBarChart, calling multiBarChart.yScale(d3.scale.log()), results in yScale is not a function.
Is there any other way to log scale multiBarChart's y axis?
Thanks
I solved this whit following approach, it's controlled by shouldLogScale var:
nv.addGraph(() => {
this.chart = nv.models.multiBarChart()
...
.y(d => {
const y = parseFloat(d.y);
return this.shouldLogScale ? Math.log10(y <= 0 ? 1 : y) : y;
});
if(this.shouldLogScale) {
this.logScale();
} else {
this.chart.yAxis.tickFormat(d3.format(`,.${this.precision}f`));
}
...
});
When initialising the chart: I modify y values, force y (chart.forceY) start and end values (map them to their log10 range), analogically I set chart.yAxis.tickValues. Lastly I map ticks (chart.yAxis.tickValues) to their original values. Just make sure to comply domain of the log function.
logScale method:
private logScale() {
this.chart.forceY([1, 180]
.map(v => Math.log10(v)));
this.chart.yAxis.tickValues([1, 3, 5, 10, 20, 50, 100, 180]
.map(v => Math.log10(v)));
this.chart.yAxis.tickFormat(d => Math.pow(10, d).toFixed(this.precision));
}
Maybe I could use a map of y domain and range values to avoid using log and pow functions in logScale method.

Matlab: Something like "relative" position with uicontrol/axis; keep fixed margins when resizing

I currently have a big headache to get a small GUI working nicely which isn't being created with GUI editor but programmatically! What I have so far is something like the following:
hFig = figure();
set(hFig, 'Position', [300 200 500 400]);
plot((1:10).^2, '*-r');
% Größe des Plots so anpassen, dass links Platz für Buttons
ap = get(gca, 'TightInset');
fp = get(gcf, 'Position');
set(gca, 'Position', [160/fp(3), 30/fp(4), (fp(3)-180)/fp(3), (fp(4)-60)/fp(4)]);
uicontrol('Style', 'pushbutton', 'String', 'foo', 'Position', [15 fp(4)-60 110 30]);
uicontrol('Style', 'pushbutton', 'String', 'bar', 'Position', [15 fp(4)-100 110 30]);
Try to resize it: It doesn't 'look' the same, which means that the uicontrol boxes don't stay at the same relative position and the margins from the axis to the figure window get bigger. What I want to achieve is:
Have a figure window with a given position (x/y, width and height) with a plot inside. The plot will have a title and labels for x and y. Make the plot as height and width to have the TightInset plus a margin in each direction of a certain px-size (e.g. TightInset + 10px) as big as the figure window; except leave 150px of free space on the left to place some uicontrol buttons, and have them stay in the same position: This would be the same as being able to give the position from top/left (top = 20, left = 10) instead of bottom/left.
Thanks a lot!
Okay finally found a working solution I wanted it to be :-) Hopefully it is helpfull for somebody interested in it:
Main script file:
p = [300 300 1000 600];
fixedMargins = [250 0 0 0]; % [left, top, right, bottom]
f = figure('Position', p, 'Color', [0.9 0.9 0.9]);
plot(-10:10, (-10:10).^3, '*-r');
set(f, 'ResizeFcn', {#resizeCallback, gca, fixedMargins, {#myuiFunc, f, 40, 50}});
title('bla')
xlabel('foooooooooo')
ylabel('barrrrrrr')
Resize Callback Function:
% Need to pass the handle of the axis to modify (hAx) AND to pass the
% desired margins as second extra callback argument:
% [left, top, right, bottom]!
function resizeCallback(hFig, ~, hAx, fixedMargins, func)
% Disable automatic rezising
set(hAx, 'Units', 'pixels');
% Figure-Size
fp = get(hFig, 'Position');
% Calculate Position of the axis
margin = get(hAx, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1];
% Position to fill the figure minus the TightInset-Margin
newPos = [0 0 fp(3:4)] - margin;
% Change position based on margins
newPos(1) = newPos(1) + fixedMargins(1);
newPos(3) = newPos(3) - fixedMargins(1) - fixedMargins(3);
newPos(2) = newPos(2) + fixedMargins(4);
newPos(4) = newPos(4) - fixedMargins(2) - fixedMargins(4);
% Set new position
set(hAx, 'Position', newPos);
% Call UI-Func
if(nargin == 5)
f = func{1};
args = func(2:end);
f(args{:});
end
end
You can pass whatever function you want to be called when resizing the figure window, e.g. to update something in the figure. In my example it's the myuiFunc(), which is the following:
function myuiFunc(hFig, left, top)
persistent handles;
if(~isempty(handles))
delete(handles);
handles = [];
end
fp = get(hFig, 'Position');
h1 = uicontrol('Style', 'pushbutton', 'String', 'Foo','Position', [left fp(4)-top 100 35]);
h2 = uicontrol('Style', 'pushbutton', 'String', 'Bar','Position', [left fp(4)-top-50 100 35]);
handles = [h1 h2];
end
I like it :) Hopefully you too!
Edit: No need to edit the resizeCallback Function! Should work if you just pass your desired margins to it and if you like, additionally a function handle with arguments which will be called for each resize!
You can also use "Normalized" units.
uicontrol('Style', 'pushbutton', 'String', 'foo', 'Units','normalized','Position', [0.90 0.05 0.08 0.08] );

Resources