To color the two regions separated by a parabolic function with different colors respectively using tikz (The function as the boundary) - tikz

Could someone give some suggestions? I want to color the two areas of the rectangle with blue and yellow respectively, as shown in the picture. The function is as the boundary between the two regions.
Here is my code and expected result.
\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{scope}[shorten >=1pt,-,draw=black!80, node distance=\layersep]
\begin{axis}[
width=8.8cm,
enlargelimits=false,
axis on top,
point meta=explicit,
height=6.5cm,
xlabel={},
ylabel={},
xtick=\empty,
ytick=\empty,
xmin=-8,
xmax=8,
ymin=-10,
ymax=1,
xlabel={\large $x$},
ylabel={\large $y$},
%ylabel style={yshift=0.0cm},
yticklabels={},
xticklabel style = {font=\large,yshift=0.0ex},
colorbar style={
font =\Large
},
]
\addplot [thick]{-0.8*(x-1)^2};
\end{axis}
\end{scope}
\end{tikzpicture}
\end{document}

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{scope}[shorten >=1pt,-,draw=black!80, node distance=\layersep]
\begin{axis}[
width=8.8cm,
enlargelimits=false,
axis on top,
point meta=explicit,
height=6.5cm,
xlabel={},
ylabel={},
xtick=\empty,
ytick=\empty,
xmin=-8,
xmax=8,
ymin=-10,
ymax=1,
xlabel={\large $x$},
ylabel={\large $y$},
%ylabel style={yshift=0.0cm},
yticklabels={},
xticklabel style = {font=\large,yshift=0.0ex},
colorbar style={
font =\Large
},
axis background/.style={fill=yellow},
]
\addplot [thick,fill=blue]{-0.8*(x-1)^2};
\end{axis}
\end{scope}
\end{tikzpicture}
\end{document}

Related

Remove first and last axis tick in pgfplots

I want to plot a coordinate system like this and I want to get rid of the first and the last tick of the x-axis and the y-axis. Like in my example I also don't want tick labels at the begin and the end of every axis.
How can I get rid of the first and the last tick of every axis?
This is my code:
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz,pgfplots}
\usepackage{helvet}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest, width=7cm, height=7cm,
every x tick label/.append style={font=\scriptsize\sffamily, yshift=0.2ex},
every y tick label/.append style={font=\scriptsize\sffamily, xshift=0.2ex}}
\begin{document}
\begin{tikzpicture}[font=\small\sffamily]
\begin{axis}[ clip=true,
xmin=-3, xmax=6,
ymin=-4, ymax=5,
xtick={-3,...,6},
xticklabels={,-2,-1,,1,2,3,4,5,},
ytick={-4,...,5},
yticklabels={,-3,-2,-1,,1,2,3,4,},
minor tick num=1,
axis lines=middle,
xlabel=\textbf{x},
ylabel=\textbf{y},
xmajorgrids=true,
xminorgrids=true,
ymajorgrids=true,
yminorgrids=true,
axis line style={line width=1pt},
every x tick/.style={color=black, thick},
every y tick/.style={color=black, thick}
]
\draw[line width=.5pt](current axis.south west)rectangle(current axis.north east);
\end{axis}
\end{tikzpicture}
\end{document}
Quick and dirty hack:
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz,pgfplots}
\usepackage{helvet}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest, width=7cm, height=7cm,
every x tick label/.append style={font=\scriptsize\sffamily, yshift=0.2ex},
every y tick label/.append style={font=\scriptsize\sffamily, xshift=0.2ex}}
\begin{document}
\begin{tikzpicture}[font=\small\sffamily]
\begin{axis}[ clip=true,
xmin=-2.99, xmax=5.99,
ymin=-3.99, ymax=4.99,
xtick={-3,...,6},
xticklabels={,-2,-1,,1,2,3,4,5,},
ytick={-4,...,5},
yticklabels={,-3,-2,-1,,1,2,3,4,},
minor tick num=1,
axis lines=middle,
xlabel=\textbf{x},
ylabel=\textbf{y},
xmajorgrids=true,
xminorgrids=true,
ymajorgrids=true,
yminorgrids=true,
axis line style={line width=1pt},
every x tick/.style={color=black, thick},
every y tick/.style={color=black, thick}
]
\draw[line width=.5pt](current axis.south west)rectangle(current axis.north east);
\end{axis}
\end{tikzpicture}
\end{document}

Matlab figure flashes back and forth between black&white and color

I'm trying to display a color disparity map, but it first displays black and white until I do 'colormap jet' then it's in color.
How can black and white image be suppressed?
imshow(disparityMap, DisparityRange );
title('DISPARITY MAP');
colormap jet;
Specify the colormap parameter to imshow.
imshow(disparityMap, DisparityRange, 'colormap', jet);
Example use
im = imread('cameraman.tif');
imshow(im, [60,200], 'colormap', hsv);
We get this image:

draw vertical lines at regular intervals in a rectangle in matlab

I need to draw vertical lines at regular intervals in a rectangular box. this is what i have used so far:
xmin=000;
xmax=70000;
ymin=0;
ymax=1000;
line1Val=900;
line2Val=600;
line3Val=300;
xlim([xmin xmax])
ylim([ymin ymax])
xl=get(gca,'XLim');
line(xl,[line1Val line1Val],'Color','y');
line(xl,[line2Val line2Val],'Color','y');
line(xl,[line3Val line3Val],'Color','y');
hold on ;
rectangle('Position',[120000,900,(280000-120000),37],'faceColor','k')
so the width of the rectangle is 160000 units i want to divide this into 4 , where the vertical line is of a different color(say red) and the height of the line is 37 units.
any ideas on how i can draw this without drawing 4 rectangles whose edges are red and are filled with black color.
You could use the parameters xstart,ystart,width and height for drawing your rectangle:
rectangle('Position',[xstart,ystart,width,height],'faceColor','k');
After that, you could determine the line positions in a loop and simply draw these lines:
for i = 1:3
x = xstart+i*width/4;
line([x x],[ystart ystart+height],'Color','r');
end
If you want a red line at the start and end of the rectangle, let i = 0:4.

How can I draw this network of tubes in Matlab?

I have a distribution of tube radius r, and I would like to plot tubes for all the sampled r in single figure as shown in the figure above. The tubes have following characteristics:
The length of all tubes is constant, but radius is varying.
The narrowest tube will be completely filled
with light gray color.
The length of the light gray color from bottom in all other tubes is
inversely proportional to the radius of the tube i.e.
length of light grey color from bottom = constant/r
The remaining length of the tube will be filled with dark gray color.
Magnitudes of r and total length of each tube is of the order of 1e-005m and 1e-002 m, respectively, so they need to be standardized compared to the X and Y axes units.
The white interspaces are just spaces and not tubes.
UPDATE (Based on the answer by Boris)
This is the code from Boris in which I made certain changes based on the characteristics of the tubes that I have described above. I am having scaling issues as I am not able to visualize my network of tubes as clearly as can be seen in the figure above.
function drawGrayTube (x, r, sigma_wn, theta, del_rho, rmin, rmax,L)
% sigma_wn is in N/m (=Kg/s^2), theta is in degrees, del_rho is in Kg/m^3
% and L is in m
h=((2*sigma_wn*cos((pi/180)*theta))./(del_rho*9.81.*r));
hmin=((2*sigma_wn*cos((pi/180)*theta))./(del_rho*9.81.*rmax));
hmax=((2*sigma_wn*cos((pi/180)*theta))./(del_rho*9.81.*rmin));
rectangle ('Position',[x,0,r/rmax,h], 'FaceColor',[0.7,0.7,0.7]);
ylim([0 1]);
if L>h
rectangle ('Position',[x,L,r/rmax,L-h], 'FaceColor',[0.3,0.3,0.3]);
ylim([0 1]);
else
rectangle ('Position',[x,L,r/rmax,L], 'FaceColor',[0.3,0.3,0.3]);
ylim([0 1]);
end
end
A simple function to draw the gray tubes could be for instance
function drawGrayTube (x, w, h)
rectangle ('Position',[x,0,w,h], 'FaceColor',[0.7,0.7,0.7]);
rectangle ('Position',[x,h,w,100-h], 'FaceColor',[0.3,0.3,0.3]);
end
Hereby, x is the x position of the tube, w denotes the width and h between 0 and 100 the height of the light gray part of the tube.
You can now use it in your example by calling
drawGrayTube (x, r, 100*constant/r)
where you have to adapt the constant such that constant/r is at most 1.
You can write a similar function for the white interspaces.
Assume that you have given a vector of radii (already scaled such that the values are between 0 and 1), e.g., r=[0.5, 0.7, 0.9, 0.1, 0.5, 0.01] then on possibility to draw the tubes is
interspace = 0.5;
for i=1:length(r)
drawGrayTube(sum(r(1:i-1))+i*interspace, 100*r(i)+1e-10, r(i)+1e-10);
end
You should use the function rectangle

How can I change the edge line color when using the 'fill' function in MATLAB?

I'm writing code in which I use MATLAB's fill command to plot 2D shapes. I can specify the fill color of the shape. However, the border line color is always black. I want the border line color the same as the fill color. How can I also specify the border line color?
See this thread:
To set the edgecolor to white do the following.
h = fill([-1 -1 1 1],[-1 1 1 -1],'w');
axis([-2 2 -2 2]);
set(h,'edgecolor','white');
that should take care of the border.
In addition to schnaader's answer, you can also set the edge color in the initial call to FILL:
hPatch = fill(xData,yData,'r','EdgeColor','r'); %# Red patch with red edges
Or stop the edges from being drawn altogether:
hPatch = fill(xData,yData,'r','EdgeColor','none'); %# Red patch with no edges