IPython Modify Plot with Container Widget - widget

I'm working with the Ipython widgets to modify dynamically plotted data, so i get the result as seen in the image; however i dont like the arrangment of the widgets (sliders and float boxes) so i have tried to align them horizontally with Box and here is the trouble because i don't know how to pass this arrangment to the function, this is how i obtain what you see in the image
#I'm just going to put two sliders
x= FloatSlider(0,1000)
y= FloatSlider(0,1000)
interactive(Function,Xo=x,Yo=y)
if i try
x= FloatSlider(0,1000,10)
y= FloatSlider(-1,1,0.1)
#this gives me the desired arrangment
co = HBox(children=[x,y])
interactive(Function,Xo=Co.children[0],Yo=Co.children[1])
this just gives me the same unordered result, and here is where i got stuck, i don't know how to input the container to make it work with my plot. Where is my error? what can i do?

The following might be a solution for your problem:
from IPython.html.widgets import *
x = FloatSlider(value = 500.0, min=0.0, max=1000.0, step=10)
y = FloatSlider(value = 0.0, min=-1.0, max=1.0, step=0.1)
plt.xlim(0,1)
plt.ylim(-1,1)
def Function(x, y):
plt.plot([0.0, 1.0], [-1.0, 1.0], 'b.')
plt.plot(x/1000.0, y, 'o')
#this gives me the desired arrangment
f = interactive(Function, x=x, y=y)
Co = HBox(f.children)
display(Co)

Related

How to find number of paths between two vertices (a1,b1), (a2,b2) on a rectangular grid in Octave?

In the image shown, number of possible routes should be calculated (for example: between (0,0) and (4,3)). Condition: no diagonal direction
I have tried this using adjacency matrix and digraph in MATALB, but I require output in Octave where digraph is not supported. Please suggest.
I actually have 8 possible ways to move between any two points:
Down, left
Down, right
Up, left
Up, right
Right, up
Right, down
Left, up
Left, down
%%%%%%%%%%%%%% Make grid points.
x = -1:6;
y = -1:5;
[xx, yy] = meshgrid(x, y, indexing = 'ij');
G = plot(xx(:),yy(:), 'b.');
grid on;
drawnow;
%%%%%%%%%% Set up figure properties: Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
%%%%%%%%%%%%%%% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
%%%%%%%%%%% Give a name to the title bar.
set(gcf, 'Name', 'Mega Constellation:Geographical separation', 'NumberTitle', 'Off');
%%%%%%%%%%%%%%% Print (x,y) values at each point.
for k = 1 : numel(xx)
str = sprintf('(%i, %i)', xx(k), yy(k));
text(xx(k), yy(k), str);
end

tutorial code for octave gives me weird error

% create GMM
mu = [0; 5];
sigma = cat(3, 1, 2);
p = [0.5; 0.5];
gmm = gmdistribution(mu, sigma, p);
% view PDF
ezplot(#(x) pdf(gmm,x));
I'm trying to run this small tutorial code since i'm new to octave
but everytime I run this code it gives me error
error: gmdistribution.pdf: X has 500 columns instead of 1
what does this error mean?
How do I fix it?
The error is effectively telling you that you passed a horizontal vector to the gmmdistribution.pdf function, instead of a vertical one, which is what it expects.
This is because ezplot internally generates a horizontal vector of 500 points on the x-axis to plot your graph.
Therefore you can 'transpose' x when you use it in your anonymous function to, get it in the right orientation:
ezplot(#(x) pdf(gmm,x.'));

What is the most comprehensible way to create a Rect object from a center point and a size in PyGame?

I want to crate an pygame.Rect object from a center point (xc, yc) and a size (w, h).
pygame.Rect just provides a constructor with the top left point and the size.
Of course I can calculate the top left point:
rect = pygame.Rect(xc - w // 2, yc - h // 2, w, h)
Or I can set the location via the virtual attribute center:
rect = pygame.Rect(0, 0, w, h)
rect.center = xc, yc
If I want to completely confuse someone, I use inflate:
rect = pygame.Rect(xc, yc, 0, 0).inflate(w, h)
Or even clamp:
rect = pygame.Rect(0, 0, w, h).clamp((xc, yc, 0, 0))
Not any of this methods satisfies me. Either I have to calculate something, I have to write several lines of code, or I have to use a function that completely hides what is happening.
I also don't want to write a function (or lambda) as I think this is completely over the top for creating a simple rectangle.
So my question is:
How do you usually create such a rectangle with a self-explanatory line of code so everyone can see what is happening at a glance?
Is there a much easier method? Do I miss something?
Interesting question Rabbid76.
I personally try to write code such that a person with only a general understanding of programming concepts can read the code. This includes absolute beginners (95% of people making PyGame questions), and converts from other languages.
This is why I mostly shy-away from using Python's if x < y < z:, and blah = [ x for x in some-complex-iff-loop ], et.al. syntax. (And that's also why I always put my if conditions in brackets.) Sure if you know python well it doesn't matter, but for an example of why it's important, go try to read a Perl script from the mid 2010's and you see stuff like:
print #$_, "\n" foreach ( #tgs );
It didn't have to be written like that, they could have used a loop-block with some instructive variable names, and not $_, etc.
So bearing the above in mind, the question comes down to - Which is the easiest to read and understand.
So for my 2-cents worth, it has to be option #2:
rect = pygame.Rect(0, 0, w, h)
rect.center = xc, yc
It's absolutely clear to a syntax-ignorant code reader that a rectangle is being created, and some kind of centre-point is being set.
But to make the code more "self documenting", it could be wrapped in a function call:
def getRectAround( centre_point, width, height ):
""" Return a pygame.Rect of size width by height,
centred around the given centre_point """
rectangle = pygame.Rect( 0, 0, w, h ) # make new rectangle
rectangle.center = centre_point # centre rectangle
return rectangle
# ...
rect = getRectAround( ( x, y ), w, h )
Sometimes more code is better.

operands could not be broadcast together with shapes

I have used this code but it showing me error. Help me solve this.
som=MiniSom(x=10,y=10,input_len=15,sigma=1.0,learning_rate=0.5)
som.random_weights_init(x)
som.train_random(data=x,num_iteration=100)
from pylab import bone, pcolor, colorbar, plot, show
bone()
pcolor(som.distance_map().T)
colorbar()
markers = ['o', 's']
colors = ['r', 'g']
for i, x1 in enumerate(x):
w = som.winner(x)
plot(w[0] + 0.5,
w[1] + 0.5,
markers[y[i]],
markeredgecolor = colors[y[i]],
markerfacecolor = 'None',
markersize = 10,
markeredgewidth = 2)
show()
The line w = som.winner(x) should be replaced with w = som.winner(x1)
MiniSom.winner() method computes the coordinates of the winning neuron for the sample x, where sample x is one single row of your dataset, and the corresponding variable name in your code is x1.
You are iterating x1 over rows of x, however still trying to use the dataset variable x with som.winner() method.

How to make errorbars with perpendicular line ends in octave

I have a question about errorbar in octave. I'm trying to plot my measure results in a plot with:
errorbar(m, my,ex, '>d')
It shows nearly what I want, except one thing, but please for the nearly description at the picture in the appendixenter image description here
I would like to have the errorbars with the perpendicular lines at the line ends
like at this picture:
https://www.math.ubc.ca/~israel/advisor/advisor5/images/h4r3.gif
Can someone give a hint?
Thanks!
If I type this in octave:
x = 1:10;
y = x.^2;
mx = (x/1.5).^-1;
errorbar (x,y,mx,'>d')
axis([-1,10,0,100]);
I get this:
Just like the picture you wanted. I don't get the angle brackets like you're getting.
EDIT: If what you're asking is for taller bars, you can do this with the following "hack":
c = get(gca,'children'); c = get(c,'children')(2);
ydata = get(c,'ydata');
ydata(4:9:end) = ydata(4:9:end) - 5; % '5' is the value to raise by
ydata(5:9:end) = ydata(5:9:end) + 5;
ydata(7:9:end) = ydata(7:9:end) - 5;
ydata(8:9:end) = ydata(8:9:end) + 5;
set(c,'ydata',ydata)