Incorrect bar graph formatting in TeX - graph

For some reason, two very simple Bar graphs look very different after compiling in tex. More precisely, on the right graph there is a distance from the columns to the lower border of the square. In the left graph, the columns are adjacent to the lower border of the square. I would like to change the left figure, to have the distance between the bottom of the square and the blue columns on the left chart, as it is implemented on the right. Do you know how to change the code of the left chart?
A picture of the graphs
\begin{figure}[H]
\begin{minipage}[h]{0.49\linewidth}
\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.15,
legend style={at={(0.75,-1)},
anchor=north,legend columns=-1},
symbolic x coords={1,2,3,4,5,6},
xtick=data,
nodes near coords,
nodes near coords align={vertical}
]
\addplot coordinates {(1, 179) (2, 122) (3, 79) (4, 59) (5, 49) (6, 205) };
\end{axis}
\end{tikzpicture}
\end{minipage}
\hfill
\begin{minipage}[h]{0.49\linewidth}
\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.15,
legend style={at={(0.75,-1)},
anchor=north,legend columns=-1},
symbolic x coords={1,2,3,4,5,6},
xtick=data,
nodes near coords,
nodes near coords align={vertical}
]
\addplot coordinates {(1, 125) (2, 106) (3, 27) (4, 49) (5, 12) (6, 5) };
\end{axis}
\end{tikzpicture}
\end{minipage}
\end{figure}

Related

Adding column values to octave bar diagram

I'm making new bar diagram in my OCTAVE application. Because of the better readings I want to add actual values for each bar. The actual values of each bar column should be located on the high Y=50% and centrally centered with each bar column. I have search over the google, but I didnt found any real example of values over the bar columns.
My example made in paint, how should look:
Picture above is a example how should look - for example purposes there are RED colored values of the each bar column.
Code of the plotting of this bar diagram:
subplot(5, 1, 5);
y2 = [data1; data2; data3; data4];
x = [1 ,2 ,3, 4];
labels = ["DATANAME1"; "DATANAME2"; "DATANAME3"; "DATANAME4"];
bar (x, y2, 0.5, 'facecolor', [0, 0.5, 1]);
set(gca, 'xticklabel', labels);
title("Something [%]");
xlabel('XLABEL');
ylabel("%");
You can use the text command for putting any text on/over any plot.
Here's my suggestion, I slightly re-worked your code:
% Data
x = [1, 2, 3, 4];
y2 = [2; 52; 3; 43];
% Original plot
bar(x, y2, 0.5, 'FaceColor', [0, 0.5, 1]);
labels = ['DATANAME1'; 'DATANAME2'; 'DATANAME3'; 'DATANAME4'];
set(gca, 'XTickLabel', labels);
title('Something [%]');
xlabel('XLABEL');
ylabel('%');
% Additional plot
y = max(y2) / 2; % "located on the high Y=50%"
values = [num2str(y2) repelem(' %', 4, 1)];
text( x - 0.1, ... % x values to put text (some negative displacement for "centrally centered with each bar column")
repelem(y, 4, 1), ... % y values to put text (four times y)
values, ... % Actual values as text plus "%" to put
'Color', 'r', ... % Red text
'FontWeight', 'bold'); % Bold text
The output then looks like this:
I used Octave 5.1.0, the repelem command might not be available before Octave 5.x, but there are alternatives to achieve what repelem does.
Notice, the construction of values (only) works, because y2 is a column vector. Otherwise, adding the "%" to each value in y2 might be a bit more complicated.
Hope that helps!

Fill under line curve

For the sample dataset below, I would like to just plot the y as a smooth line with fill under the line using R.
I am able to get smooth line but not the color filled curve. Could someone please help me here?
date, y
2015-03-11, 71.12
2015-03-10, 34.0
2015-03-09, 11.1
2015-03-08, 9.62
2015-03-07, 25.97
2015-03-06, 49.7
2015-03-05, 38.05
2015-03-04, 38.05
2015-03-03, 29.75
2015-03-02, 35.85
2015-03-01, 30.65
The code I used to plot the smooth line is as follows. I am unable to get fill the portion under the line with a color
y <- df$y
x <- 1:length(y)
plot(x, y, type='n')
smooth = smooth.spline(x, y, spar=0.5)
lines(smooth)
EDIT
Using the polygon function does not give what is expected. The shaded area should be below the line not above
with(smooth, polygon(x, y, col="gray"))
Describe a polygon by listing its boundary vertices in order as you march around it.
This polygon's boundary consists of the curve plus two more vertices at the bottom right and bottom left. To help you see them, I have overplotted the vertices, varying their colors by position in the sequence.
Here is the R code that did it. It used predict to obtain coordinates of the curve from the spline object, then adjoined the x- and y-coordinates of the two extra points using the concatenation operator c. To make the filling go to the axis, the plot range was manually set.
y <- c(71, 34, 11, 9.6, 26, 50, 38, 38, 30, 36, 31)
n <- length(y)
x <- 1:n
s = smooth.spline(x, y, spar=0.5)
xy <- predict(s, seq(min(x), max(x), by=1)) # Some vertices on the curve
m <- length(xy$x)
x.poly <- c(xy$x, xy$x[m], xy$x[1]) # Adjoin two x-coordinates
y.poly <- c(xy$y, 0, 0) # .. and the corresponding y-coordinates
plot(range(x), c(0, max(y)), type='n', xlab="X", ylab="Y")
polygon(x.poly, y.poly, col=gray(0.95), border=NA) # Show the polygon fill only
lines(s)
points(x.poly, y.poly, pch=16, col=rainbow(length(x.poly))) # (Optional)

Tikzpicture - Arrow next to/under plot label

I am looking for a help with Latex. I'm new here so unfortunately I'm not allowed to post pictures yet.
I have a tikzpicture plot with labeled axes (x and y). Now I would like to add an arrow below the x-axis to indicate the direction of the subsequential treatments. In addition I would like to have an arrow left of the y-axis label pointing upwards. This arrow should have a second label saying "relative increase in Fe".
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=1]
\begin{axis}[
%legend style={at={(1.05,0.05)}, %gibt Ort für Legende an
%anchor=south west},
%axis x line=bottom, % erzeugt x-Achse mit Pfeil
%axis y line=left, %
width=15.5cm,
height=10cm,
%scaled ticks=false,
%ymin=0,
xmin=-0.5,
xmax=5,
ymin=0,
ymax=5,
xtick={0,1,2,3,4},
xticklabels={Fe2O3,1,2,3,4},
bar width=50pt,
%ytick={},
%yticklabels={},
%use un%%ts,
%x unit=-,
%x unit prefix=,
%y unit=\frac{m}{s},
%y unit prefix=,
xlabel=Subsequential Treatments over Time ,
ylabel=Auger Peak to Peak Height Ratio Fe:O,
x tick label style= {rotate=90,anchor=east},
ybar stacked]
\addplot [draw=white, very thin]
coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
\addplot [draw= blue, fill=blue]
coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
%\node at (100,1) [orange!50!yellow]{\small{ZnO-h}};
\end{axis}
\end{tikzpicture}
\caption[Auger Spectrum of HOPG]{Auger Peak to Peak Height Ratios of Fe:O at an primary electron beam of \unit{2.0}{keV}.}
\label{Auger_ratio_histogram_}
\end{figure}
The code below illustrates two techniques for placing such arrows. The starting point of both is to add name=MyAxis to the axis options, which allows you to refer to the anchor points of the axis as with a normal node. A pgfplots axis also has anchors such as outer north east, which lies outside the axis descriptions, while north east lies on the corner of the axes.
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
name=MyAxis,
%legend style={at={(1.05,0.05)}, %gibt Ort für Legende an
%anchor=south west},
%axis x line=bottom, % erzeugt x-Achse mit Pfeil
%axis y line=left, %
width=15.5cm,
height=10cm,
%scaled ticks=false,
%ymin=0,
xmin=-0.5,
xmax=5,
ymin=0,
ymax=5,
xtick={0,1,2,3,4},
xticklabels={Fe2O3,1,2,3,4},
bar width=50pt,
%ytick={},
%yticklabels={},
%use un%%ts,
%x unit=-,
%x unit prefix=,
%y unit=\frac{m}{s},
%y unit prefix=,
xlabel=Subsequential Treatments over Time ,
ylabel=Auger Peak to Peak Height Ratio Fe:O,
x tick label style= {rotate=90,anchor=east},
ybar stacked]
\addplot [draw=white, very thin]
coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
\addplot [draw= blue, fill=blue]
coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
%\node at (100,1) [orange!50!yellow]{\small{ZnO-h}};
\end{axis}
\draw [-latex] ([yshift=-2ex]MyAxis.outer south west) --node[below]{Direction of subsequential treatments} ([yshift=-2ex]MyAxis.outer south east);
\draw [-latex] (MyAxis.outer south west) ++(-2ex,0) coordinate(start) --node[sloped,above] {relative increase in Fe} (start |- MyAxis.outer north west);
\end{tikzpicture}
\end{document}

How to isolate grid lines on top of a curve Mathematica

I am looking to plot a graph which has a semi circle from -3 to 3 on the x axis and 0 to 3 on the y axis. I would like for the semi circle and its interior to be untouched but the rest of the graph be filled with grid lines (with a point with increments of .06 for x and y values). I have been able to plot a graph with the semi circle and the grid lines but can't seem to figure out how to clear the semi circle of grid lines.
Any help would be greatly appreciated!
Here's what I have so far, with f[x] being the function for the semicircle and the xPoints and yPoints being the x and y coordinates.
Plot[f[x], {x, -3, 3},
GridLines -> {{xPoints},{yPoints}}]
try working with RegionPlot
RegionPlot[ x^2+y^2 < 9 , {x,-3,3 }, {y,0,3} , Mesh->10 , AspectRatio->Automatic,
MeshShading->{{None,None},{None,None}} ]
( Just switch the inequality to get the grid outside the curve )

How to specify the margin of a plot in mm/cm using matplotlib?

Example
Suppose I have two triangles:
A triangle with points (0, 0), (10, 0), (10, 0.5) and
a triangle with points (0, 0), (1, 0), (0.5, 11)
The resulting two plots without specifying the xlim and ylimlook like this:
Question
What do I need to do to satisfy all points listed below?
Make the triangle visible, so that no line of the triangle is hidden by an axis
Specify the same margin for all plots in mm, cm or other unit.
(in the example above only two triangles were used. Actually I have n triangles.)
As margin I mean the distance between the outer points of the triangle and the axis.
The resulting plots should look like this
with the difference that the distances, which are marked with the red arrows, should all be the same!
I don't know of a way to to it in cm/mm, but you can do it with the precentage of the total size:
# you don't really need this see bellow
#from matplotlib.backends.backend_pdf import PdfPages
import pylab
import matplotlib.pyplot as plt
left,bottom,width,height = 0.2,0.1,0.6,0.6 # margins as % of canvas size
fig = plt.figure(figsize=(4,4),facecolor="yellow") # figure size in Inches
fig.patch.set_alpha(0.8) # just a trick so you can see the difference
# between the "canvas" and the axes
ax1 = plt.Axes(fig,[left,bottom,width,height])
ax1.plot([1,2,3,4],'b') # plot on the first axes you created
fig.add_axes(ax1)
ax1.plot([0,1,1,0,0], [0,0,1,1,0],"ro") # plot on the first axes you created
ax1.set_xlim([-1.1,2])
ax1.set_ylim([-1.1,2])
# pylab.plot([0,1,1,0,0], [0,0,1,1,0],"ro") avoid usig if you
# want to control more frames in a plot
# see my answer here
#http://stackoverflow.com/questions/8176458/\
#remove-top-and-right-axis-in-matplotlib-after-\
#increasing-margins/8180844#8180844
# pdf = PdfPages("Test.pdf")# you don't really need this
# pylab.savefig(pdf, papertype = "a4", format = "pdf")
# automagically make your pdf like this
pylab.savefig("Test1.pdf", papertype="a4",facecolor='y')
pylab.show()
pylab.close()
# pdf.close()
and the output is:
corrected image:
Your two triangles with points (0, 0), (10, 0), (10, 0.5) and (0, 0), (1, 0), (0.5, 11) would be represented in pylab as:
Ax = [0, 10, 10]
Ay = [0, 0, 0.5]
Bx = [0, 1, 0.5]
By = [0, 0, 11]
pylab.plot(Ax, Ay)
pylab.plot(Bx, By)
Let's see what the lowest X value is:
lowestX = None
for x in Ax+Bx:
if lowestX==None or x<lowestX:
lowestX = x
Exercise for the reader to do the same for highestX, lowestY, and highestY.
Now, consider a boundary of 2 units, you can add / subtract these units from the lowest and highest values and set xlim and ylim:
margin = 2
pylab.xlim([lowestX-margin, highestX+margin])
pylab.ylim([lowestY-margin, highestY+margin])

Resources