I need to plot all the elements of a[n] 0<=n<=30. Is that possible in maxima?
These are the numbers i need to plot
by the way, you can help others help you by typing your work so far into your question. That way people can see it immediately instead of having to go look for it.
About plotting the points, remember that a[n] as you have defined it is a so-called array function instead of a list or array. The plotting functions can work with lists, I believe. So you need to evaluate your function at a number of points and then plot the points.
Something like: mypoints: makelist([n, a[n]], n, 1, 30); to generate the points. Then with the points in hand, there are different ways to plot them. You can try: plot2d([discrete, mypoints]); There are other ways; see wxdraw2d for example.
Related
In Julia I know one way of combining subplots as the this:
p1=plot(...)
p2=plot(...)
p3=plot(...)
plot(p1, p2, p3, layout(3,1))
However, suppose that I don't know beforehand the number of subplots, like when I generate the subplots based of some parameters passed to a function, then how would I accomplish the same thing?
What I tried was that I made an array to contain all the subplots
Plot_array=[p1, p2, p3]
(again, the number of elements of Plot_array can be changed based on what passed to a function)
and then
plot(Plot_array, layout=(...))
However, this did not work. Does anyone know any better way?
Something like this should work
plot(Plot_array..., layout=(length(Plot_array), 1))
Three dots is so called "splat" operator: https://docs.julialang.org/en/v1/manual/functions/#Varargs-Functions
I am integrating a vector differential equation in Julia (the number of components is of the order of magnitude of 50). When I plot this solution, obviously this plot will be very crowded if I plot all the components, so I want to plot only let's say the first 10 components. I can't find how to do this. Also, instead of plotting the output for each component, I would like to plot not the output of the integration at each time step, but the hyperbolic tangent of this. I also don't find how to do this manipulation. I think the key problem is that I don't understand/find what kind of object the solution of a differential equation is in julia.
Here is a minimal working example that integrates the differential equation and plots the whole solution.
using DifferentialEquations
using Plots
using LinearAlgebra
N=50
J=0.18*randn(Float64,N,N)
g=1
function hDerivative(timederiv,h,p,t)
for i=1:length(h)
timederiv[i] = -h[i]
for j=1:length(h)
timederiv[i]=+timederiv[i]+J[i,j]*tanh(g*h[j])
end
end
end
function pltTimeVolution()
hinit=rand(Float64,N)
tspan=(0.0,50)
prob = ODEProblem(hDerivative,hinit,tspan)
sol=solve(prob)
plot(sol)
print(sol)
end
pltTimeVolution()
For help, take a look at the solution handling page: http://diffeq.sciml.ai/latest/basics/solution.html . The solution is both an abstract array and a continuous function. So you can for example get a continuous solution at 10000 evenly spaced time points like t=range(tspan[1],stop=tspan[2],length=10000), do A = sol(t) and that will give a 50x10000 timeseries of each solution at evenly spaced time points, and then sol[i,:] would be the timeseries of the ith variable at the time points t. You can use that to build the arrays you want an plot them. Full code like this:
t=range(tspan[1],stop=tspan[2],length=10000)
A = sol(t)
ts10 = sol[10,:]
Another way to do this is to solve with something like saveat=t, in which case the solution is an array which saves at those time points. This looks like:
t=range(tspan[1],stop=tspan[2],length=10000)
sol=solve(prob,saveat=t)
ts10 = sol[10,:]
Or you can make use of the plot recipe which does this kind of handling internally. Take a look at http://diffeq.sciml.ai/latest/basics/plot.html#Choosing-Variables-1 . You can do things like plot the first variable and then lay over the plot of the 10th variable like:
plot(sol,vars=(0,1))
plot!(sol,vars=(0,10))
The ! means its the mutating plot function, i.e. modify the previous plot by adding a new series. Then as noted in the documentation there are shortcuts, like
plot(sol,vars = [1, 3, 4])
will plot the 1st, 3rd, and 4th variables as functions of time. In this form, you can also specify what kind of transformation you want as a function. That's mentioned in the same place in the documentation. For example, the two-dimensional plot of time vs tanh would be the transformation function f=(t,y)->(t,tanh(y)), i.e. how you take in the 2D variable and spit out the 2D variable to plot is just pass through the first and tanh the second. Then using this, you'd use the plot command:
f=(t,y)->(t,tanh(y))
plot(sol,vars = (f,0,5))
to plot the tanh of the 5th variable against time.
These are a few different ways to manipulate the solution argument and generate the plots you need. Pick the one that works best for you.
Currently trying to write a simple r script that when passed in 2 vectors of values would calculate some relationship between them (in the given case, r_square) and display it in a graph with the best fit line.
temp1 <- sample(20000,1367,replace=F,prob=NULL)
temp2 <- sample(20000,1367,replace=F,prob=NULL)
fit <- lm(temp1 ~temp2)
plot(temp1,temp2,ann="true")
abline(fit)
(here using sample in lack of real data).
The problem is that i'm trying to add interactivity which would display point's value (X/Y coordinates of sort) on hover.
I've managed to find a few functions that identify them by the their order in the vectors (HWidentify, identify, etc). But none of them give the actual value (x,y) so i was wondering if it's possible to print out coordinates that aren't permanent.
If you're trying to print the coordinates, you could use the labels argument to the identify() function.
identify(temp1, temp2, labels=paste(temp1, temp2, sep=","))
The HWidentify function also has a labels argument that you can set to whatever you want, using the paste function like #JeanV.Adams works similarly and then you have the hover functionability.
Is there an function in R that does the same job as Matlab's "bar" function?
R does have a "barplot" function in the library graphics, however, it is not the same.
The Matlab bar(X,Y) (verbatim excerpt from MATLAB documentation) "draws a bar for each element in Y at locations specified in X, where X is a vector defining the x-axis intervals for the vertical bars." (emphasis mine)
However, the R barplot function does not allow one to specify locations.
Perhaps there is a method in ggplot2 that supports this? I am only able to find standard bar charts in ggplot2.
No, barplot is not the same as bar, but you should read the whole help. You can do many things to position the bars. The first is simply their order in Y. You could insert spaces if you wish (additional 0s). If you have X and Y then sort Y on X (Y[order(X)]) and plot it. If you need to change positions use the "space" and "width" arguments. It's not as straightforward as specifying X values I suppose but it's definitely more useful in most situations. Generally what you want to adjust is widths of bars and spaces between bars. Their position on the X-axis should be arbitrary. If the position on the X-axis is really meaningful then you should be using line plots, not bar graphs.
In R:
barplot(rbind(1:10, 2:11), beside=T, names.arg=1:10)
In MATLAB:
>> bar(1:10, [(1:10)' (2:11)'])
Read up on par . Then observe, for example:
x<-c(1,2,4,5,6)
y<-c(3,4,3,4,2)
plot(x,y,type='h',lwd=6)
Edit: yes, I know this doesn't (yet) plot multiple data sets, but I would hope you can see simple ways to make that happen, with spacings, colors, etc. specified to your exact liking :-)
Sounds vaguely like the R stepfun. On the other hand one would need to know what "draws a bar" means before saying it is not the same as barplot(..., horiz=TRUE) One would, of course, need to examine some more detailed evidence such as data and plots before arriving at a conclusion, however. #John Colby should be congratulated for adding some specificity to the discussion. The axis function is probably what Quant Guy needs education regarding.
When doing matrix operations, I would like to be able to see what the results of my calculations are, at least to get a rough idea of the nature of the matrices going in and coming out of the operation.
How can I plot a matrix of real numbers, so that the x axis represents columns, the y represents rows, and the color or size of a point represents the cell value?
Ultimately, I would like to display multiple plots, e.g. the right and left hand sides of an equation.
Here is some example code:
a <- matrix(rnorm(100), ncol = 10)
b <- diag(1,10)
c <- a*b
par(mfrow = c(1,3))
plot.matrix.fn <- function(m) {
#enter answer to this question here
}
lapply(list(a,b,c), plot.matrix.fn)
update: since posting this question, I found that there are some great examples here: What techniques exists in R to visualize a "distance matrix"?
You could try something like (adjusting the parameters to your particular needs)
image(t(m[nrow(m):1,] ), axes=FALSE, zlim=c(-4,4), col=rainbow(21))
producing something like
See ?image for a single plot (note that row 1 will be at the bottom) and ?rasterImage for adding 1 or more representations to an existing plot. You may want to do some scaling or other transformation on the matrix first.
Not an answer but a longer comment.
I've been working on a package to plot matrices using grid.raster, but it's not quite ready for release yet. Your example would read,
library(gridplot)
row_layout(a, b, c)
I found that writing custom functions was probably easier than tweaking 10s of parameters in lattice or base graphics, and ggplot2 lacks some control over the axes.
However, writing graphics functions from scratch also means reinventing non-trivial things like layout and positioning; hopefully Hadley's scales and guides packages can make this easier. I'll add the functions to gridExtra when the overall design seems sound and more stable.