GNUPLOT: How to use a function to decide the plot style - function

I have lots of series to draw in 1 plot. I want to decide the plot style/type according to the number of a series. Here is the example code of what I am aiming for:
title(i)=value(sprintf("title%i",i))
title1='x'
title2='x^2'
title3='x^3'
...
with(i)=value(sprintf("with%i",i))
with1='lines'
with2='points'
with3='boxes'
...
plot for [i=1:100] '-' title title(i) with with(i)
I have test the title() function works. But the with function does not work correctly.
Can you help me fix the code?
Or do you think there are other ways to achieve what I am aiming for?

It's possible to build a string of a plot command and then run this string:
p="plot x linewidth 1"
do for [i=1:10]{
p=p.sprintf(", x+%d linewidth %d", i, i+1)
}
print p
eval(p)
The output of the print command is:
plot x linewidth 1, x+1 linewidth 2, x+2 linewidth 3, ...
and this is the resulting plot:
Depending on what you finally want, this might be something for you:
do for [i=1:10]{
p=p.(i==1 ? "'myData.csv' with lines" : "")
p=p.(i==2 ? "'myData.csv' with steps" : "")
...
}
However, this looks pretty ugly.

Related

Extracting color from complex function: " Cannot modify global variable 'cColor' in function."

I'd like to extract the "col" color value from this function to be used to paint plots or candle colors. But everything I try creates one error or another. I checked the Script Reference. Shouldn't there be some way to "return" a value, as is usually the case with most functions?
lset(l,x1,y1,x2,y2,col)=>
line.set_xy1(l,x1,y1)
line.set_xy2(l,x2,y2)
line.set_width(l,5)
line.set_style(l, line.style_solid)
line.set_color(l,y2 > y1 ? #ff1100 : #39ff14) //red : green
temp = line.get_price(l,bar_index) // another value to extract
The documentation is showing it like this:
line.new(x1, y1, x2, y2, xloc, extend, color, style, width) → series line
So in your code it's looking differently and also the "new" is missing.
Scrolling a bit up on the linked page shows that there exist indeed methods to retrieve some properties of the line object:
Lines are managed using built-in functions in the line namespace. They include:
line.new() to create them.
line.set_*() functions to modify the properties of an line.
line.get_*() functions to read the properties of an existing line.
line.copy() to clone them.
line.delete() to delete them.
The line.all array which always contains the IDs of all
the visible lines on the chart. The array’s size will depend on
the maximum line count for your script and how many of those you
have drawn. aray.size(line.all) will return the array’s size.
The most simple usage is to instantiate a line object with the correct values directly, like shown here:
//#version=5
indicator("Price path projection", "PPP", true, max_lines_count = 100)
qtyOfLinesInput = input.int(10, minval = 1)
y2Increment = (close - open) / qtyOfLinesInput
// Starting point of the fan in y.
lineY1 = math.avg(close[1], open[1])
// Loop creating the fan of lines on each bar.
for i = 0 to qtyOfLinesInput
// End point in y if line stopped at current bar.
lineY2 = open + (y2Increment * i)
// Extrapolate necessary y position to the next bar because we extend lines one bar in the future.
lineY2 := lineY2 + (lineY2 - lineY1)
lineColor = lineY2 > lineY1 ? color.lime : color.fuchsia
line.new(bar_index - 1, lineY1, bar_index + 1, lineY2, color = lineColor)
Getting the line color from outside is difficult or impossible though as there never exists a method to retrieve it while for other properties those methods exist.
So the most simple way is to create the same funcionality, to get the color that exists inside the line-object, outside too, or only outside.
currentLineColor = y2 > y1 ? #ff1100 : #39ff14
You could try to extend the line-object somehow like this:
line.prototype.get_color = function() {
return this.color;
};
console.log(line.get_color())
I'm not sure if the approach with the prototype is working but it's worth it to try if you need it.

Use of function / return

I had the task to code the following:
Take a list of integers and returns the value of these numbers added up, but only if they are odd.
Example input: [1,5,3,2]
Output: 9
I did the code below and it worked perfectly.
numbers = [1,5,3,2]
print(numbers)
add_up_the_odds = []
for number in numbers:
if number % 2 == 1:
add_up_the_odds.append(number)
print(add_up_the_odds)
print(sum(add_up_the_odds))
Then I tried to re-code it using function definition / return:
def add_up_the_odds(numbers):
odds = []
for number in range(1,len(numbers)):
if number % 2 == 1:
odds.append(number)
return odds
numbers = [1,5,3,2]
print (sum(odds))
But I couldn’t make it working, anybody can help with that?
Note: I'm going to assume Python 3.x
It looks like you're defining your function, but never calling it.
When the interpreter finishes going through your function definition, the function is now there for you to use - but it never actually executes until you tell it to.
Between the last two lines in your code, you need to call add_up_the_odds() on your numbers array, and assign the result to the odds variable.
i.e. odds = add_up_the_odds(numbers)

Ploting a function with BigFloats values in Julia

I have a function ( lets call it test(M) ), that "spits out" only BigFloats, like shown below.
test(100)
22.48487818538224586950434052086250350042005040604872584710165219826379165499864
I would like to plot this function. Howver, it seems that this doesn't work for big Floats.
I even tried to use round(Int8, test(x)) in order to make the results smaller, but it still didn't work.
using Plots
M = 1:10
plot(test.(M), xaxis=:log)
It works for a random function I made up as below:
using Plots
test(x) = BigFloat(rand()) * 100 + x * rand()
M = 1:10
plot(test.(M), xaxis=:log)
So, perhaps your test() function is not returning a proper vector of BigFloats, ie has Inf or such?

Get an yerror plot without a line in Octave

I'd like to print a plot with y-error-bars and just plain points. My current Octave script looks like this:
errorbar(x_list, y_list, Delta_y_list, "~.x");
title("physikalisches Pendel");
xlabel("a^2 [m^2]");
ylabel("aT^2 [ms^2]");
print -dpdf plot.pdf
The plot I get has a line, although I specified the .x style option:
http://wstaw.org/m/2012/04/14/umbrella5.png
How can I get rid of that line?
And the ylabel is in the scale as well, is there some way to fix that?
One has to set the linestyle:
p1 = errorbar(plot_x, plot_y, plot_error, "~.k");
set(p1, "linestyle", "none");
set(p1, "marker", "+");

Python: For loop problem

I have a PSP page with html embedded. I need to place another for loop so i can insert another %s next to background-color: which will instert a appropriate colour to colour in the html table.
For example i need to insert for z in colours so it can loop over the colours list and insert the correct colour. Where ever i try to insert the for loop it doesnt seem to work it most commonly colours each cell in the table 60 times then moves onto the next cell and repeats itself and crashes my web browser.
The colours are held in a table called colours.
code below:
<table>
<%
s = ''.join(aa[i] for i in table if i in aa)
for i in range(0, len(s), 60):
req.write('<tr><td><TT>%04d</td>' % (i+1));
for k in s[i:i+60]:
req.write('<TT><td><TT><font style="background-color:">%s<font></td>' % (k));
req.write('</TT></tr>')
#end
%>
</table>
-----EDITED-----
Plugged in the code provided ebo, it colours the table all one colour. The colours list contains a variety of colours e.g. colour = ['yellow', 'yellow', 'yellow', 'yellow', 'red', 'red', 'red', 'red']
<table>
<%
s = ''.join(aa[i] for i in table if i in aa)
for i in range(0, len(s), 60):
req.write('<tr><td>%04d</td>' % (i+1));
for j, k in enumerate(s[i:i+60]):
req.write('<td><font style="background-color:%s;">%s<font></td>' % (colour[j % len(colour)], k));
req.write('</tr>')
#end
%>
</table>
I guess you want one color for each column. Best idea would be to use enumerate:
s = ''.join(aa[i] for i in table if i in aa)
for i in range(0, len(s), 60):
req.write('<tr><td>%04d</td>' % (i+1))
for j, k in enumerate(s[i:i+60]):
req.write('<td style="background-color: %s;">%s</td>' %
(colours[j % len(colours)], k))
req.write('</tr>')
I stripped all the TT tags. They were mostly wrong, either not closed or spanning over other elements.
Update This should do. Take a look at the source, if every field is filled correctly. Also download Firebug and take a look at the parsed html code. It may differ depending on your other html failures.
colour = ["red", "red", "green", "yellow"]
print "<table>"
s = '1234567890'
for i in range(0, len(s), 60):
print('<tr><td>%04d</td>' % (i+1));
for j, k in enumerate(s[i:i+60]):
print('<td><font style="background-color:%s;">%s<font></td>' % (colour[j % len(colour)], k));
print('</tr>')
print "</table>"
I piped the output of the following code into a html file and opened it. Works as expected.
python table.py > table.html
firefox table.html
I guess you have some additional errors in your code which mess up parsing.