Plotting using multiple variables in gnuplot - variables

I have a datafile with multiple columns, the first two indicating the position and the others indicating other properties (such as number of items sent from this point). eg:
1 1 1 57.11
2 1 2 62.40
3 4 1 31.92
What I want to do is plot the points at the positions, but use values from the other columns to vary point type and size (for example). However I can't seem to find a way to reference columns in the plot. I know of the use of "variable", but I cant find a way to use multiple variables.
What I want is something like the following:
plot "mydata" using 1:2 notitle with points pt ($3) ps ($4/10)
so that pt and ps use the value for each point taken from the third and fourth columns respectively.
Is this even possible in gnuplot? Is there some sort of work-around?

You should be able to use the keyword variable to do something like this:
plot 'datafile' using 1:2:3:4 w points ps variable lc variable
Or possibly mapping the value to a palette:
plot 'datafile' using 1:2:3:4 w points ps variable lc palette
The keyword variable and/or palette causes gnuplot to read the properties from the file and they both require an extra column to be read via using. Of course all the usual stuff with using applies -- You can apply transforms to the data, etc:
plot 'datafile' using 1:2:3:($4+32.) w points ps variable lc palette
I don't remember off the top of my head whether the 3rd column will be the pointsize or the color here, and I don't have time right now to play around with it to figure it out. You can do the experimenting and post a comment, or I'll come back to this when I have time and add an update.
Some of the other properties (e.g. pointtype) can't be changed quite to easily using variable. The easiest way to do this is to use filters with the gnuplot ternary operator.
First, write a function that returns a pointtype based on the data from 1 column of the datafile:
my_point_type(x) = x
Here I use a simple identity function, but it could be anything. Now, you can loop over the pointtypes you want (here 1-10) making a plot for each:
plot [for PT=1:10] 'datafile' u 1:((my_point_type($3) == PT) ? $2:NaN) with points pt PT
This assumes that the column with pointtype information is the 3rd column and that the second column holds the position information. This can also be combine with the stuff that I demonstrated above.

Related

Using plotArrays in Dymola to plot the data over different x-axis values

I need to draw a graph like this:
I have used plotArrays two times(1.to get curves on the left, 2.to get curves on the right) to get the curves on in two separate plot-windows.
plotArrays(x_neg,SOC_neg,legend=names,id=1);
plotArrays(x_pos,SOC_pos,legend=names,id=2);
The middle region is empty. If I use single plotArrays function combining these data, the ends will automatically connect with each other which I don't want to do.
How can I plot it in single command?
Thank you
Use multiple calls to the function plotArray with the same id and erase=false.

Is it possible to use only certain values in a column when performing a pandas query

I'm trying to use a post/zipcode map to plot Longitude/Latitude coordinated using GeoPandas.
If the post/zipcodes are sequential i.e. see below code, then I have no issues but there are some outlying post/zipcodes I'd like to use i.e 5118, 5371-3 but also redundant, 5354, as well as they are or are not required to be viewed on the map.
df_a = df.query('code >= 5350 & code <= 5355')
ax = df_a.plot()
Can I add or remove post/zipcodes within the one line of code above in a query?
Map with marker points

How to efficiently append a dataframe column with a vector?

Working with Julia 1.1:
The following minimal code works and does what I want:
function test()
df = DataFrame(NbAlternative = Int[], NbMonteCarlo = Int[], Similarity = Float64[])
append!(df.NbAlternative, ones(Int, 5))
df
end
Appending a vector to one column of df. Note: in my whole code, I add a more complicated Vector{Int} than ones' return.
However, #code_warntype test() does return:
%8 = invoke DataFrames.getindex(%7::DataFrame, :NbAlternative::Symbol)::AbstractArray{T,1} where T
Which means I suppose, thisn't efficient. I can't manage to get what this #code_warntype error means. More generally, how can I understand errors returned by #code_warntype and fix them, this is a recurrent unclear issue for me.
EDIT: #BogumiłKamiński's answer
Then how one would do the following code ?
for na in arr_nb_alternative
#show na
for mt in arr_nb_montecarlo
println("...$mt")
append!(df.NbAlternative, ones(Int, nb_simulations)*na)
append!(df.NbMonteCarlo, ones(Int, nb_simulations)*mt)
append!(df.Similarity, compare_smaa(na, nb_criteria, nb_simulations, mt))
end
end
compare_smaa returns a nb_simulations length vector.
You should never do such things as it will cause many functions from DataFrames.jl to stop working properly. Actually such code will soon throw an error, see https://github.com/JuliaData/DataFrames.jl/issues/1844 that is exactly trying to patch this hole in DataFrames.jl design.
What you should do is appending a data frame-like object to a DataFrame using append! function (this guarantees that the result has consistent column lengths) or using push! to add a single row to a DataFrame.
Now the reason you have type instability is that DataFrame can hold vector of any type (technically columns are held in a Vector{AbstractVector}) so it is not possible to determine in compile time what will be the type of vector under a given name.
EDIT
What you ask for is a typical scenario that DataFrames.jl supports well and I do it almost every day (as I do a lot of simulations). As I have indicated - you can use either push! or append!. Use push! to add a single run of a simulation (this is not your case, but I add it as it is also very common):
for na in arr_nb_alternative
#show na
for mt in arr_nb_montecarlo
println("...$mt")
for i in 1:nb_simulations
# here you have to make sure that compare_smaa returns a scalar
# if it is passed 1 in nb_simulations
push!(df, (na, mt, compare_smaa(na, nb_criteria, 1, mt)))
end
end
end
And this is how you can use append!:
for na in arr_nb_alternative
#show na
for mt in arr_nb_montecarlo
println("...$mt")
# here you have to make sure that compare_smaa returns a vector
append!(df, (NbAlternative=ones(Int, nb_simulations)*na,
NbMonteCarlo=ones(Int, nb_simulations)*mt,
Similarity=compare_smaa(na, nb_criteria, nb_simulations, mt)))
end
end
Note that I append here a NamedTuple. As I have written earlier you can append a DataFrame or any data frame-like object this way. What "data frame-like object" means is a broad class of things - in general anything that you can pass to DataFrame constructor (so e.g. it can also be a Vector of NamedTuples).
Note that append! adds columns to a DataFrame using name matching so column names must be consistent between the target and appended object.
This is different in push! which also allows to push a row that does not specify column names (in my example above I show that a Tuple can be pushed).

Keeping table formatting in Sage with multiple tables

As the title suggests, I am trying to keep proper table formatting in Sage while displaying multiple tables (this is strictly a formatting question, so no knowledge of the math involved is necessary). Currently, I am using the following code:
my_table2 = table([column1, column2], frame = True)
my_table1 = table([in_the_cone, lengths_in_cone], frame = True)
result_table1 = my_table1.transpose()
result_table2 = my_table2.transpose()
result_table1
result_table2
With this, I receive no output for table1 and the following output for table2:
I want both tables to look this way, but having no output for the first table is no good. So I tried changing the bottom two lines to:
result_table1, result_table2
While this does display both tables, the formatting now looks like:
Is there a way I can display both tables at the same time with the first formatting?
It would have been nice for you to include a full minimal working example, but in any case it does depend a little on the output.
Basically, in a notebook or other "cell", only the last return value prints to the screen in some fashion (sometimes via a "hook" as in your case). But if you use the comma, that implicitly creates a "tuple" which is then printed as a tuple, so you lose that "hook" to display things with math modes (since a tuple doesn't have that).
In this case, the (newish) canonical way to achieve what you want is
pretty_print(result_table1)
pretty_print(result_table2)
though you may want to put print "\n" in between so they don't end up right on top of each other.
Edit: Here is a picture in Jupyter inside of Sage.

How to change the range of my x-axis in matplotlib

I am trying to plot a list of 30.000 values. The name of the list is "velocity_x". I just plot them with the following command:
plot(velocity_x,'r')
the result is shown in the image below (do not pay attention to the dashed line)
Since I am using that command line, it creates automatically a x-axis of length 30.000. What I would like to do is changing the range of my x-axis in such a way to show the time(s) instead of the iterations where t = 0.0002 * iteration.
You could use linspace:
a=np.linspace(0,6,len(velocity_x))
plot(a, velocity_x, 'r' )
You can set the values by setting 'Xticklabel' see here.
You want to do something like
h=gca; %get current axis if you dont have it as a handle already.
set(h, 'Xticklabel', 0:5000*0.0002:5000*0.0002*7); %set correct ticks.
You could also try something along the lines of
plot( [i*0.0002 for i in range(len(velocity_x))], velocity_x, 'r' )