Bargraph Error in Tikzpicture - tikz

I don't know why the data on x-axis over leaping. I don't have any idea.
\documentclass[varwidth=true, border=2pt]{standalone} \usepackage{pgfplots}\begin{document}
\begin{tikzpicture}
\begin{axis}[
symbolic x coords={Coke-Classic,Diet Coke, Dr.Peppre,Pepsi Cola,Sprite,},
xtick=data
]
\addplot[ybar,fill=blue] coordinates {
(Coke-Classic, 38)
(Diet Coke, 16)
(Dr.Peppre, 10)
(Pepsi Cola, 26)
(Sprite, 10)
};
\end{axis}
\end{tikzpicture} \end{document}

There are two possible solutions, that came to my mind. Both are relatively easy (only one line has to be added):
Solution 1
To avoid overlapping x-labels, you can explicitly define the size of one x-unit:
\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
symbolic x coords={Coke-Classic,Diet Coke, Dr.Peppre,Pepsi Cola,Sprite,},
xtick=data,
x=2cm
]
\addplot[ybar,fill=blue] coordinates {
(Coke-Classic, 38)
(Diet Coke, 16)
(Dr.Peppre, 10)
(Pepsi Cola, 26)
(Sprite, 10)
};
\end{axis}
\end{tikzpicture}
\end{document}
The only thing I changed is adding x=2cm to the axis-options.
The result looks as follows:
Solution 2
The solution above increases the width of the plot. If you don't want this, you can instead rotate the tick labels:
\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
symbolic x coords={Coke-Classic,Diet Coke, Dr.Peppre,Pepsi Cola,Sprite,},
xtick=data,
xticklabel style={rotate=45}
]
\addplot[ybar,fill=blue] coordinates {
(Coke-Classic, 38)
(Diet Coke, 16)
(Dr.Peppre, 10)
(Pepsi Cola, 26)
(Sprite, 10)
};
\end{axis}
\end{tikzpicture}
\end{document}
Here I just replaced the x=2cm with xticklabel style={rotate=45} (45 is the rotation angle, you can use any other angle, just use what you think looks best).

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}

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

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}

Precise and smooth curve with Tikz

I'm trying to draw some curves, but it doesn't work out very well.
what I need
what I get
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ticks=none,
xtick distance=1,
ytick distance=1,
axis equal image=true,
grid,
grid style={gray!50},
grid=both,
xlabel={$x$},
ylabel={$y$},
axis lines=middle,
xmin=-4, xmax=9, ymin=-5, ymax=4,
axis x line=center,
axis y line=center,
]
\addplot[thick, smooth] plot coordinates
{
(-3, -1)
(-.5, -3)
(.5, -1.9)
(1.5, -2.8)
(3.5, 1)
(5.5, 3)
(7.5, -1.95)
(8, -1.5)
};
\end{axis}
\end{tikzpicture}
\end{document}
Is it possible to build such smooth curves without adding a large number of points? In the original drawing, you can see several reference points. Is there any way to configure \addplot or any other command?
Using the added information that all those points (except the last one) have zero gradient, Bézier curves seem better suited than a smooth plot.
So, you may achieve this kind of thing...
with this bit of code...
\documentclass[border=2pt]{standalone}
\usepackage{fourier}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[thick, line cap=round, >=latex, scale=0.5]
% Some constants for the Bezier curve
\def\x{0.5}
\def\u{1.0}
\def\v{0.3}
\def\w{0.1}
% The grid, axes and labels
\draw[thin, gray!20, help lines] (-4.5 ,-4.5) grid (9.5 ,4.5);
\draw[very thick, ->] (-4.5, 0) -- (9.8, 0) node[above] {$x$};
\draw[very thick, ->] (0, -4.5) -- (0, 4.8) node[left] {$y$};
\path (-3, 0) coordinate (x1) node[above] {$-3$}
(0, 0) coordinate (x2) node[below] {$\quad0$}
(1, 0) coordinate (x3)
(8, 0) coordinate (x4) node[below] {$8$}
(0, 1) coordinate (y1) node[right] {$1$};
% ticks are really cumbersome
\draw[very thick] ($(x1)+(0,-\w)$) -- ($(x1)+(0,\w)$);
\draw[very thick] ($(x3)+(0,-\w)$) -- ($(x3)+(0,\w)$);
\draw[very thick] ($(x4)+(0,-\w)$) -- ($(x4)+(0,\w)$);
\draw[very thick] ($(y1)+(-\w,0)$) -- ($(y1)+(\w,0)$);
% Finally, the points...
\path (-3, -1) coordinate (A)
(-.5,-3) coordinate (B)
(.5, -1.9) coordinate (C)
(1.5,-2.8) coordinate (D)
(3.5, 1) coordinate (E)
(5.5, 3) coordinate (F) node[above] {$y=F(x)$}
(7.5,-1.95) coordinate (G)
(8, -1.5) coordinate (H);
% and the line with suitable gradients (after a bit trial and error)
\draw[ultra thick]
(A) ..controls +(\u, 0) and ( $(B) + (-\x, 0)$ )..
(B) ..controls +(\x, 0) and ( $(C) + (-\v, 0)$ )..
(C) ..controls +(\v, 0) and ( $(D) + (-\v, 0)$ )..
(D) ..controls +(\x, 0) and ( $(E) + (-\u, 0)$ )..
(E) ..controls +(\u, 0) and ( $(F) + (-\x, 0)$ )..
(F) ..controls +(\x, 0) and ( $(G) + (-\u, 0)$ )..
(G) ..controls +(\w, 0) and ( $(H) + (-\w,-\v)$ )..
(H);
% two small circles to mark the curve ends
\draw[thin, fill=white] (A) circle (3pt);
\draw[thin, fill=white] (H) circle (3pt);
\end{tikzpicture}
\end{document}

Matlab - change axis multiplier

Could you tell me how can I change an axis "multiplier"? I mean a value I circled in the picture, let's say I would like to have x10^3 instead of x10^4.
As of R2015b it is part of the Numeric Ruler Properties:
ax = get(gca);
ax.YAxis.Exponent = -3;
I have little bit tricky solution:
Set YTickMode to manual.
Set your own YTickLabel.
Place the text on top with your desired multiplier.
here is:
set(gca, 'YTickMode', 'manual');
set(gca, 'YTickLabel', get(gca,'YTick') / 1000);
text(0, 1.02 * get(gca,'YLim')(2), 'x 10^3');
Play with the multiplier 1.02 in the third line to place your text to the good place.
You can use the following Matlab Central tick2text: create easy-to-customize tick labels
http://www.mathworks.com/matlabcentral/fileexchange/16003-tick2text-create-easy-to-customize-tick-labels
along with the sprintf formatting.
Scale your data by 0.1, which gives you the multiplier you want.
Then override the tick labels so that tick 1.0 is labelled 10, etc.

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