I am using Octave plot() function to plot scatter points on a 2D graph. And then I am using the contour() function to draw a contour on top of the points. But the contour() function is not overlapping on top of the points. What happens is that the scatter plot graph is replaced entirely with the contour, even though I am using the HOLD ON command.
I have something like this:
plot(); %plot the x,y scatter plot
hold on; %hold on to be able to add to the plot
contour(); %Add the contour on top of the scatter plot
I wonder if someone can show some sample code they can show to add a contour to an existing plot.
thanks
Here is an example :
x = [-10:0.1:10];
y = x .^ 2;
z = x' * x;
hold on;
contour(x,y,z);
plot(x,y);
Will yield this graph (in blue you can see the parabol issued by plot).
Related
In R, I am able to perfectly create a contour plot and a separate graph of various functions. However, when I go to plot them together, because the contour plot has a key to the right of the graph that does not exist in the other graph, the function graph axes are distorted, and the lines go off the graph.
For example, I graph the line y=10000-x, and my x-axis goes from 0 to 10000. This graphs fine on its own, but when I add it to the contour plot graph, even though the contour plot graph has the same scale, the graph of y=10000-x goes off of the graph, because the location of the x=10000 area of the graph has shifted to the right to make room for the key.
I have attached my code a photo that better illustrates the problem.
x = seq(0, 10000,length.out=100)
y = seq(0, 10000,length.out=100)
z<- outer(x,y,function(x,y) (339-0.01*x-0.003*y)*x+(399-0.01*y-0.004*x)*y-(400000+195*x+225*y))
filled.contour(x, y, z,add=TRUE,col=hcl.colors(25, "Spectral"))
eq1 = function(x){(0*x)+8000} #equation goes in the brackets
curve(eq1, from=0, to=10000,add=TRUE,xlab="", ylab="",ylim=c(0,10000),col="purple",lwd=3)
eq2 = function(x){10000-x} #equation goes in the brackets
curve(eq2, from=0, to=10000,add=TRUE,ylim=c(0,10000),col="blue",lwd=3)
abline(v=5000,col='green',lwd=3)
abline(v=0,col='black',lwd=3)
eq1 = function(x){(0*x)} #equation goes in the brackets
curve(eq1, from=0, to=10000,add=TRUE,ylim=c(0,10000),col="black",lwd=3)
text(2000,4000,labels="feasible region")
title(main="Constraints of TV setup",xlab="Number of 19'' sets", ylab="Number of 21'' sets")
points(3846,6154,col="red",pch=16)
text(3846,6800,labels="max",col='red')
N = function(x,y){(339-0.01*x-0.003*y)*x+(399-0.01*y-0.004*x)*y-(400000+195*x+225*y)}
cat(N(3846,6154))
Is it possible to use a log scale in a contour plot? The plot should look like contour(log10(data)), but with correct labelling of the colorbar.
I have a scatterplot in a log/log space
plot(a,b,log="xy")
or in ggplot2
qplot(a,b,data="time",log="xy")
Now I would like to impose upon this scatter plot the curve f(x)=x*x+2. Butthe function woudl need to be plotted in the logarithmic space as well. How would I do this? Is there an way to do this in ggplot2?
As you guessed, curve is the command that you're looking for in base graphics.
#Make up some data
set.seed(0)
a <- 1:10
b <-(a^2+2)*exp(0.1*rnorm(10))
plot(a,b,log='xy')
curve(x^2+2,add=TRUE)
in ggplot2 world:
qplot(a,b,data=time)+stat_function(fun=function(x){x^2+2}) + coord_trans(xtrans = "log10",ytrans="log10")
from Plotting in R using stat_function on a logarithmic scale seems to do what you're after.
I have a data.frame of x/y/z points. I know how to make a 3d scatterplot using the rgl package but I would like to connect each point in the scatterplot to make a wireframe or surface plot.
This code returns the scatter plot
library(rgl)
Data <- expand.grid(x=seq(0,10),y=seq(0,10))
Data$z <- Data$x^2+Data$y^2
plot3d(Data)
While this code returns a blank graph:
plot3d(Data,type='wire')
I can make the plot I want with lattice:
library(lattice)
wireframe(z~x+y,Data)
I can even make it rotate:
library(TeachingDemos)
rotate.wireframe(z~x+y,Data)
But I prefer rgl over lattice because it renders much quicker and lets you rotate the plot with the mouse.
Whats the proper way to make a wireframe plot in rgl?
The surface drawing plot function in rgl is persp3d and like base::persp, it needs a matrix as input to the z argument
zmat <- matrix(Data$z, 11,11)
persp3d(x=seq(0,10), y=seq(0,10), z=zmat)
I did spin this graphic a bit before capturing it with a screen grabbing program I use:
How to plot the density of a single column dataset as dots? For example
x <- c(1:40)
On the same plot using the same scale of the x-axis and y-axis, how to add another data set as line format which represent the density of another data that represents the equation of
y = exp(-x)
to the plot?
The equation is corrected to be y = exp(-x).
So, by doing plot(density(x)) or plot(density(y)), I got two separated figures. How to add them in the same axis and using dots for x, smoothed line for y?
You can add a line to a plot with the lines() function. Your code, modified to do what you asked for, is the following:
x <- 1:40
y <- exp(-x)
plot(density(x), type = "p")
lines(density(y))
Note that we specified the plot to give us points with the type parameter and then added the density curve for y with lines. The help pages for ?plot, ?par, ?lines would be some insightful reading. Also, check out the R Graph Gallery to view some more sophisticated graphs that generally have the source code attached to them.