Octave one line two plot differ from multiple line plot - plot

I'm fitting some data, but when I plot the data and fit in one-line-plot (see left figure) the drawn graph is correct
plot(x, y, '.b;data;', [0.05 2], phi(1)+phi(2)*[0.05 2], '--r;fit;', [0.05 2]);
But when I used two separated plot (see right figure) the graph differs from mention above
hold on;
plot(x, y, '.b;data;', [0.05 2]);
plot(phi(1)+phi(2)*[0.05 2], '--r;fit;', [0.05 2]);
hold off;
grid on;
Data
[x, y]
ans =
0.050000 3571.000000
0.100000 6567.000000
0.200000 12760.000000
0.300000 20512.000000
0.400000 25480.000000
0.500000 32088.000000
1.000000 63223.000000
2.000000 128690.000000
Calculate Linear Regression
A = [N, sum(x); sum(x), sum(x.*x)];
b = [sum(y); sum(x.*y)];
phi = inv(A)*b;
Is there any way to solve this?

When the x-axis values are not specified then 1:numel(y) are considered to be the x-axis values.
In your code:
%Your first graph:
plot(x, y, '.b;data;', [0.05 2], phi(1)+phi(2)*[0.05 2], '--r;fit;', [0.05 2]);
%^^^^^^^
%Your second graph:
plot(x, y, '.b;data;', [0.05 2]);
%^^^^^^^^^
plot(phi(1)+phi(2)*[0.05 2], '--r;fit;', [0.05 2]);
%!!!!!!!!!!!!!!!!!!!!!! %^^^^^^^^^
The parts highlighted with ^ in above plot commands have x=[1 2] and y=[0.05 2] and the part highlighted with ! has x=[1 2] and y=phi(1)+phi(2)*[0.05 2]. You can see those lines being drawn if you zoom at that area.
So your first plot command should be:
plot(x, y, '.b;data;', [0.05 2], phi(1)+phi(2)*[0.05 2], '--r;fit;');
and it should be split like this:
plot(x, y, '.b;data;');
hold on;
plot([0.05 2], phi(1)+phi(2)*[0.05 2], '--r;fit;');

Related

How to change length of a side of polygon ensure the resulting polygon is similar shape

Polygon
I have a polygon that has only 90 and 45-degree angles, now I want to change the length of any side and adjust the polygon so that results should be a similar polygon.
Like this:
Shape after side change
import numpy as np
import matplotlib.pyplot as plt
# an arbitrary polygon, for example a pentagon:
P = np.array([[0.2, 0.1], [1, -0.2], [3, 1], [2, 0.7], [0.5, 1], [0.2, 0.1]])
P[-1, ] = P[0, ]
# pick a side between, say the one between vertices P[i,] and P[i+1,],
# and assume you want to make it of length a = 3:
i=2
a=3
# pick a point V0 in the plane, where a vertex P[j,] of the scaled polygon should be places:
j=1
v0 = [1,1]
# calculate the scale, which is a / (length of edge P[i,] P[i+1,]):
scale = a / np.linalg.norm(P[i+1, ] - P[i,])
# scale and position the polygon where you want it to be:
P_scaled = scale * P + (v0 - P[j,])
# plot polygon P:
plt.figure()
plt.plot(P[:,0], P[:,1])
for k in range(P.shape[0]):
plt.plot(P[k,0], P[k,1], 'ro')
# plot polygon P_scaled:
plt.plot(P_scaled[:,0], P_scaled[:,1])
for k in range(P_scaled.shape[0]):
plt.plot(P_scaled[k,0], P_scaled[k,1], 'ro')
axx = plt.gca()
axx.set_aspect('equal')
plt.show()

plot multiple dependence data in one graph in Julia

I'm trying to plot on Julia, here is the sample data Ecdat Airline. I want to compare the airline 1~6's cost, but the x-axis was wrong(should be 1970-1984), and my code seems too dirty, maybe it could be plot in one line.
using Plots, UnicodePlots, RDatasets
unicodeplots()
v = dataset("Ecdat","Airline")
plot(v.Cost[v.Airline .== 1])
plot!(v.Cost[v.Airline .== 2])
plot!(v.Cost[v.Airline .== 3])
plot!(v.Cost[v.Airline .== 4])
plot!(v.Cost[v.Airline .== 5])
plot!(v.Cost[v.Airline .== 6])
The output
┌────────────────────────────────────────────────────────────┐
4.88870026e6 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠄⠀│ y1
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠔⠁⠀⠀│ y2
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠤⠊⠀⠀⣀⠄⠀│ y3
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⠤⠔⠊⠁⣀⠤⠒⠉⠀⠀⠀│ y4
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠒⠒⠊⠉⠉⠀⢀⣀⠤⠒⠉⠀⠀⠀⠀⠀⠀⠀│ y5
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠊⠀⠀⠀⠀⡠⠒⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ y6
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠔⠁⠀⠀⢀⠔⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⠁⠀⠀⢀⠎⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠊⠀⠀⢀⠔⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠤⠊⠁⠀⠀⡠⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡠⠔⠊⠁⠀⠀⠀⡠⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⠤⠒⠁⠀⠀⠀⠀⠀⡠⠔⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⠤⠤⠒⠊⠁⠀⠀⠀⠀⠀⣀⡠⠔⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠤⠔⠒⠉⠁⠀⠀⠀⠀⠀⢀⣀⠤⠔⠊⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀│
│⠀⠀⠀⢀⣀⣀⡠⠤⠤⠒⠊⠉⠀⠀⠀⠀⠀⣀⡠⠤⠒⠊⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⡠⠤⣤⣒⣒⣉⣁⡀⠀│
│⠀⠈⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⡠⠔⠒⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⡠⠤⠔⣒⡲⠮⠛⠛⠉⠓⠊⠉⠉⠀⠀⠀⢀⣀⠄⠀│
│⠀⠀⠀⠀⠀⠀⢀⣀⡠⠤⠒⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⡠⠤⠔⠒⠊⠉⣁⡠⠔⠒⠉⠀⠀⠀⠀⠀⠀⠀⠀⣀⡠⠤⠒⠊⠁⠀⠀⠀│
│⠀⠐⠒⠒⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣀⣀⠤⠤⠤⠤⠒⠒⣒⣊⣉⣉⣁⠤⠤⠤⠔⠒⠊⠉⠀⠀⠀⠀⣀⣀⣠⡤⠤⠴⠶⠮⠭⠔⠒⠒⠒⠒⠒⠒⠂⠀│
│⠀⠠⠤⠤⠔⠒⣒⣒⣒⡲⠶⠶⠮⠛⠛⠛⠓⠒⠒⠊⠉⠉⠉⠉⠉⣉⣀⣀⣀⣀⣀⣀⣤⣤⣤⣔⡲⠶⠮⠛⠛⠋⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
-71402.26000000001 │⠤⠼⠿⠿⠿⠿⠶⠶⠶⠶⠶⠶⠶⠾⠿⠿⠿⠭⠭⠭⠭⠭⠭⠭⠭⠭⠭⠭⠭⠭⠭⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤│
└────────────────────────────────────────────────────────────┘
0.5800000000000001 15.42
To plot DataFrames, use StatsPlots rather than Plots (it's an extension of Plots to DataFrames and statistical types). Also you don't need to import UnicodePlots directly. Finally, you're looking for the group keyword.
Your code should be
using StatsPlots, RDatasets
unicodeplots()
v = dataset("Ecdat","Airline")
#df v plot(:Year, :Cost, group = :Airline)

R Plot - Y Axis and plotted values are not the same (values from txt file)

I am somewhat new to R and I am trying to plot values from real data .
x axis is the time and y axis real values but in graph y axis is between 0 and 100, but the real values of y are different.
I need your help.Thank you
y <- c(9.21368, 8.90582, 8.92218, 8.53934, 7.99324, 7.53952, 7.51255, 7.1065, 6.91431,
6.9704, 7.92692, 7.74947, 7.10618, 6.50479, 5.9741, 5.08136, 4.32405, 3.86809,
3.4377, 3.54596, 3.07406, 2.92242, 1.67285, 1.11221, 0.501673)
t <- 1 : length(y)
plot(t, y)
# Length(y) and length(t) must be same.
You can view your y-axis values in the plot, by adding text function
y <- c(9.21368, 8.90582, 8.92218, 8.53934, 7.99324, 7.53952, 7.51255, 7.1065, 6.91431,
6.9704, 7.92692, 7.74947, 7.10618, 6.50479, 5.9741, 5.08136, 4.32405, 3.86809,
3.4377, 3.54596, 3.07406, 2.92242, 1.67285, 1.11221, 0.501673)
t <- 1: length(y)
plot(t,y)
text(t,y,pos=3, cex=0.7)
The y-axis is always like the x axis but in reality is not like that:

Plotting raster images using custom colours in R

This might sound like a strange process, but its the best I can think of to control rasterised colour gradients with respect to discrete objects (points, lines, polygons). I'm 95% there but can't quite plot correctly.
This should illustrate proof of concept:
require(raster)
r = matrix(56:255, ncol=20) # reds
b = t(matrix(56:255, ncol=10)) # blues
col = matrix(rgb(r, 0, b, max=255), ncol=20) # matrix of colour strings
ras = raster(r) # data raster object
extent(ras) = extent(1,200,1,100) # set extent for aspect
plot(ras, col = col, axes=F, asp=T) # overwrite data with custom colours
Here I want to clip a raster to a triangle and create colour gradient of pixels inside based on their distances to one of the sides. Sorry for length but its the most minimal example I can design.
require(raster); require(reshape2); require(rgeos)
# equilateral triangle
t_s = 100 # half side
t_h = floor(tan(pi*60/180) * t_s) # height
corners = cbind(c(0, -t_s, t_s, 0), c(t_h, 0, 0, t_h))
trig = SpatialPolygons(list(Polygons(list(Polygon(corners)),"triangle")))
# line to measure pixel distances to
redline = SpatialLines(list(Lines(Line(corners[1:2,]), ID='redline')))
plot(trig); plot(redline, add=T, col='red', lwd=3)
# create a blank raster and clip to triangle
r = raster(mat.or.vec(nc = t_s*2 + 1, nr = t_h))
extent(r) = extent(-t_s, t_s, 0, t_h)
r = mask(r, trig)
image(r, asp=T)
# extract cell coordinates into d.f.
cells = as.data.frame(coordinates(rasterToPoints(r, spatial=T)))
# calculate distance of each pixel to redline with apply
dist_to_line = function(xy, line){
point = readWKT(paste('POINT(', xy[1], xy[2], ')'))
gDistance(point, line) / t_h
}
cells$dists = apply(cells, 1, dist_to_line, line=redline)
cells$cols = rgb(1 - cells$dists, 0, 0)
length(unique(cells$cols)) # count unique colours
# use custom colours to colour triangle pixels
image(r, col = cells$cols, asp=T)
plot(r, col = cells$cols, asp=T)
As you can see the plotting fails to overwrite as in the first example, but the data seems fine. Trying to convert to matrix also fails:
# try convertying colours to matrix
col_ras = acast(cells, y~x, value.var='cols')
col_ras = apply(col_ras, 1, rev) # rotate acw to match r
plot(r, col = col_ras, asp=T)
Very grateful for any assistance on what's going wrong.
Edit:
To show Spacedman's plotRGB method:
b = brick(draster, 1-draster, 1-draster)
plotRGB(b, scale=1)
plot(trig, col=NA, border='white', lwd=5, add=T)
Easy way is to go from your points to a spatial pixels data frame to a raster, then do the colour mapping...
Start with:
> head(cells)
x y dists
1 0.0000000 172.5 0.0014463709
2 0.0000000 171.5 0.0043391128
3 -0.9950249 170.5 0.0022523089
4 0.0000000 170.5 0.0072318546
5 0.9950249 170.5 0.0122114004
convert:
> coordinates(cells)=~x+y
> draster = raster(as(cells,"SpatialPixelsDataFrame"))
colourise:
> cols=draster
> cols[!is.na(draster)]= rgb(1-draster[!is.na(draster)],0,0)
> plot(cols, col=cols)
I'm not sure this is the right way to do things though, you might be better off creating an RGB raster stack and using plotRGB if you want fine colour control.

plot vertical color line in R

I would like to plot multiple color vertical line for a time series as below,
[1,] 4.698478 0
[2,] 4.698205 1
[3,] 4.698569 0
[4,] 4.697385 -1
...
to plot a blue color vertical line when [,2] is 1, and a red line when [,2] is -1, wonder if someone can help, thanks!
You can do the following, assuming data[, 1] is the x-value for the vertical line:
abline(v = data[data[, 2] == 1, 1], col = 'blue')
abline(v = data[data[, 2] == -1, 1], col = 'red')
Nothing wrong with #ChristopherLouden's answer, but this would be a way to do it in a single call to abline:
abline(v=m[,1], col=c('red', NA, 'blue')[as.numeric(as.factor(m[,2]))])
I ended up using quantmod's charting function, pretty good results.
chartSeries(price[,1])
addTA(price[,2]==1,pch=1, on=1,col="blue")
addTA(price[,2]==-1,pch=1, on=1,col="red")

Resources