xlabel overlap in clustered histogram in gnuplot - alignment

Here is my gnuplot script:
set terminal pngcairo font "arial,10" fontscale 1.0 size 500, 350
set output 'hist.png'
set border 3 front linetype -1
set boxwidth 0.8 absolute
set style fill pattern
set grid nopolar
set key top left reverse
set style histogram rowstacked
set style data histograms
set xtics nomirror font ",8"
set ytics 2 nomirror norotate autojustify
set ylabel font ",18" "Number of pieces used"
set xlabel font ",18" "Utilization" offset 0,-2
plot newhistogram "1.3", 'input.data' using 2:xtic(1) t "used" lc rgbcolor "black" lt 1 fs pattern 4, '' u 3 t "usable" lc rgbcolor "black" lt 1 fs pattern 1, '' u 4 t "wasted" lc rgbcolor "black" lt 1 fs pattern 3, \
newhistogram "1.5", '' u 5:xtic(1) notitle lc rgbcolor "black" lt 1 fs pattern 4, '' u 6 notitle lc rgbcolor "black" lt 1 fs pattern 1, '' u 7 notitle lc rgbcolor "black" lt 1 fs pattern 3, \
newhistogram "1.7", '' u 8:xtic(1) notitle lc rgbcolor "black" lt 1 fs pattern 4, '' u 9 notitle lc rgbcolor "black" lt 1 fs pattern 1, '' u 10:xtic(1) notitle lc rgbcolor "black" lt 1 fs pattern 3, \
newhistogram "1.8", '' u 11:xtic(1) notitle lc rgbcolor "black" lt 1 fs pattern 4, '' u 12 notitle lc rgbcolor "black" lt 1 fs pattern 1, '' u 13:xtic(1) notitle lc rgbcolor "black" lt 1 fs pattern 3, \
newhistogram "2.0", '' u 14:xtic(1) notitle lc rgbcolor "black" lt 1 fs pattern 4, '' u 15 notitle lc rgbcolor "black" lt 1 fs pattern 1, '' u 16:xtic(1) notitle lc rgbcolor "black" lt 1 fs pattern 3
for the input file:
A 16 0 0 18 1 0 18 0 3 14 1 8 10 5 10
B 16 0 0 17 2 0 15 6 0 9 14 0 0 25 0
C 16 0 0 18 1 0 19 2 0 12 11 0 2 23 0
It generates the following figure:
How can I make the xlabel "Utilization" be a little lower than the "1.7" label? Using offset moves both labels together.

Just shift the titles belonging to the newhistogram up a bit with:
set style histogram rowstacked title offset 0,1
(the rest of the script remains unchanged).

I like #Christoph's answer, but here are a couple of other workarounds:
1) You can change the xlabel to "\nUtilization", which will add a newline and move the text to the next row.
2) You can also use a regular label and turn off the xlabel, which can allow more flexibility:
unset xlabel
set label "Utilization" at graph 0.5,0.1 font ",18"

Related

AMPL ERROR: I am getting this error, i have mentioned and attached mod and dat files

Below are my dat and mode files for ampl .
I am getting the following error:
hw3.dat, line 14 (offset 262):
b[1] already defined
context: 1 1 >>> ; <<<
hw3.dat, line 14 (offset 262):
b[1] already defined
context: 1 1 >>> ; <<<
hw3.dat, line 14 (offset 262):
b[1] already defined
context: 1 1 >>> ; <<<
hw3.dat, line 14 (offset 262):
b[1] already defined
context: 1 1 >>> ; <<<
hw3.dat, line 14 (offset 262):
b[1] already defined
MODEL FILE:
# AMPL model for the Minimum Cost Network Flow Problem
#
# By default, this model assumes that b[i] = 0, c[i,j] = 0,
# l[i,j] = 0 and u[i,j] = Infinity.
#
# Parameters not specified in the data file will get their default values.
reset;
options solver cplex;
set NODES; # nodes in the network
set ARCS within {NODES, NODES}; # arcs in the network
set english;
set french;
param b {NODES} default 0; # supply/demand for node i
param c {ARCS} default 0; # cost of one of flow on arc(i,j)
param l {ARCS} default 0; # lower bound on flow on arc(i,j)
param u {ARCS} default Infinity; # upper bound on flow on arc(i,j)
var x {ARCS}; # flow on arc (i,j)
maximize cost: sum{(i,j) in ARCS} c[i,j] * x[i,j]; #objective: minimize
#arc flow cost
subject to flow_balance {i in NODES}:
sum{j in NODES: (i,j) in ARCS} x[i,j] - sum{j in NODES: (j,i) in ARCS}
x[j,i] = b[i];A
subject to capacity {(i,j) in ARCS}: l[i,j] <= x[i,j] <= u[i,j];
subject to flow_conservation {i in english}:
sum{j in french} x[i,j] = 1;
subject to flow_bounds {(i,j) in ARCS}:
x[i,j] = 0 || x[i,j] <= 1;
#subject to Number: {(i,j) in ARCS} x[i,j]=0 || x[i,j] = 1;
data hw3.dat
solve;
printf "The optimal pair assignments with compatibility scores are: \n";
for {i in english, j in french} {
printf "English Child %d and French Child %d with compatibility score %d \n", i, j, c[i,j];
}
data;
set NODES :=e1 e2 e3 f1 f2 f3;
set ARCS:= (e1,f1) (e1,f2) (e1,f3) (e2,f1) (e2,f2) (e2,f3) (e3,f1) (e3,f2) (e3,f3);
set english:=e1 e2 e3;
set french:=f1 f2 f3;
param: b:=
1 1
1 1
1 1
1 1
1 1
1 1;
param: c l u:=
[e1,f1] 6 0 10
[e1,f2] 3 0 10
[e1,f3] 2 0 10
[e2,f1] 9 0 10
[e2,f2] 5 0 10
[e2,f3] 1 0 10
[e3,f1] 4 0 10
[e3,f2] 10 0 10
[e3,f3] 8 0 10
;
It keeps saying that b is already defined, but i didnt do it. i tried changing the name from b to some other thing, still shows the same error.
can someone help please.
In your data file you have:
param: b:=
1 1
1 1
1 1
1 1
1 1
1 1;
Each line means b[1] = 1 and that is why you are getting the error "b[1] already defined context".
Since b is indexed over NODES (param b {NODES} default 0;) you should have something like the following instead:
param: b :=
e1 1
e2 1
e3 1
f1 1
f2 1
f3 1;

Variable labels on multiple lines in LaTeX when exporting results with esttab

I am trying to export some regression results to LaTeX using the community-contributed command estate in Stata. I want to use variable labels instead of variable names, but some of the labels are so long that the width of the table does not fit nicely on the page once I export it to LaTeX.
Example data can be found below:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double areakey float(state d1 d2) int(bfunemp otnenon bmunemp)
1015001100 1 0 1 0 26 0
1033020701 1 0 1 27 1 33
1073003803 1 0 1 208 0 78
1073005702 1 0 1 76 0 88
1073011904 1 0 1 35 0 44
1073013200 1 0 1 11 0 59
1097006407 1 0 0 0 0 9
1097007300 1 0 1 9 0 0
1111000600 1 0 0 6 0 1
1115040201 1 0 0 4 0 2
1115040204 1 0 0 3 5 6
1117030317 1 1 1 0 0 0
1117030320 1 0 0 11 0 0
1117030336 1 0 0 0 0 0
1125011600 1 0 1 91 0 88
1125011701 1 0 1 73 0 41
4013112100 4 0 0 0 0 0
4013112301 4 0 1 8 0 25
4013112601 4 0 1 0 0 0
4013112700 4 0 0 0 0 0
end
label var areakey "GEOID10"
label var state "State FIPS code"
label var d1 "Distance <= 1 Dummy"
label var d2 "Distance <= 2 Dummy"
label var bfunemp "Unemployed Black/Afr. Am. females 16+ yo."
label var otnenon "Pers. 18-64 yo. other lang. not well/no Eng."
label var bmunemp "Unemployed Black/Afr. Am. males 16+ yo."
In addition, the code I am using is the following:
global xvars bfunemp otnenon bmunemp
eststo mreg1: quietly areg d1 $xvars, absorb(state) robust ///
cluster(state)
eststo mreg2: quietly areg d2 $xvars, absorb(state) robust ///
cluster(state)
keep if indicatorvar > 0
eststo mreg3: quietly areg d1 $xvars, absorb(state) robust ///
cluster(state)
eststo mreg4: quietly areg d2 $xvars, absorb(state) robust ///
cluster(state)
esttab mreg1 mreg3 mreg2 mreg4 using ///
"$repodir/output/tables/tract_mregs.tex", ///
replace booktabs longtable mtitles("y1" "y1" "y2" "y2") ///
s(modelsample modelobs, label("Sample" "N")) se ///
noconstant nonumbers nonotes label star(* 0.10 ** 0.05 *** 0.01)
The result after I compile the TeX file is a table where the left margin is fine but the contents of the table extend off the right side of the page such that there is no margin.
Is there a way of specifying the format so that the labels can extend to multiple lines?
Substituting option longtable with wrap will produce the desired output after you compile:
esttab mreg1 mreg3 mreg2 mreg4, tex wrap ///
mtitles("y1" "y1" "y2" "y2") ///
s(modelsample modelobs, label("Sample" "N")) se ///
noconstant nonumbers nonotes label star(* 0.10 ** 0.05 *** 0.01)
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{4}{c}}
\hline\hline
&\multicolumn{1}{c}{y1}&\multicolumn{1}{c}{y1}&\multicolumn{1}{c}{y2}&\multicolumn{1}{c}{y2}
> \\
\hline
Unemployed & -0.0000359 & -0.0000359 & -0.000489 & -0.000489 \\
Black/Afr. Am. females 16+ yo.& (0.0000876) & (0.0000876) & (0.000770) & (0.000770)
> \\
[1em]
Pers. 18-64 yo. & -0.00555\sym{**} & -0.00555\sym{**} & 0.0244\sym{**} & 0.0244\sym{**} \\
other lang. not well/no Eng.& (0.000143) & (0.000143) & (0.00126) & (0.00126)
> \\
[1em]
Unemployed & -0.00180\sym{*} & -0.00180\sym{*} & 0.0109 & 0.0109 \\
Black/Afr. Am. males 16+ yo.& (0.000221) & (0.000221) & (0.00194) & (0.00194)
> \\
\hline
Sample & & & & \\
N & & & & \\
\hline\hline
\end{tabular}
}
In addition, the option varwidth() can also help adjusting the length of the first column.

Using esttab with ttest

I am running a ttest command and exporting results to LaTeX using estpost and the community-contributed command esttab.
I am testing for a difference for means (of variable height, by child gender) for several years and would like the years to be displayed vertically (in rows) rather than horizontally.
My code and is given below:
foreach i in 2009 2010 2013 {
use "`i'.dta", clear
global year `i'
eststo _$year : estpost ttest height, by(child_gender)
}
esttab . using "trends.tex", nonumber append
Data for 2009:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(child_gender height)
0 156
1 135
0 189
1 168
0 157
1 189
1 135
1 145
0 124
1 139
end
Data for 2010:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(child_gender height)
0 151
1 162
0 157
1 134
0 157
1 189
1 135
1 145
0 143
1 166
end
Data for 2013:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(child_gender height)
0 177
0 135
0 189
0 168
0 157
1 189
1 135
1 145
1 124
1 127
end
I would like the output arranged as follows (but in LaTeX):
Any suggestions on how to make this work?
The way to do this can be found below. You need to play with the options to further polish the table.
First define the program append_ttests, which is a quickly modified version of appendmodels, Ben Jann's program for stacking models:
program append_ttests, eclass
version 8
syntax namelist
tempname b V tmp
foreach name of local namelist {
qui est restore `name'
mat `tmp' = e(b)
local eq1: coleq `tmp'
gettoken eq1 : eq1
mat `tmp' = `tmp'[1,"`eq1':"]
local cons = colnumb(`tmp',"_cons")
if `cons'<. & `cons'>1 {
mat `tmp' = `tmp'[1,1..`cons'-1]
}
mat `b' = nullmat(`b') , `tmp'
mat `tmp' = e(t)
mat `tmp' = `tmp'["`eq1':","`eq1':"]
if `cons'<. & `cons'>1 {
mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
}
capt confirm matrix `V'
if _rc {
mat `V' = `tmp'
}
else {
mat `V' = ///
( `V' \ ///
`tmp' )
}
}
mat `b' = `b''
mat A = `b' , `V'
mat rown A = `0'
ereturn matrix results = A
eret local cmd "append_ttests"
end
Then run your loop and append the t-tests:
foreach i in 2009 2010 2013 {
use "`i'.dta", clear
estpost ttest height, by(child_gender)
estimates store year`i'
}
append_ttests year2009 year2010 year2013
See the results as follows:
esttab e(results), nonumber mlabels(none) ///
varlabels(year2009 2009 year2010 2010 year2013 2013) ///
collabels("Height" "t statistic")
--------------------------------------
Height t statistic
--------------------------------------
2009 4.666667 .3036859
2010 -3.166667 -.2833041
2013 21.2 1.415095
--------------------------------------
Add the tex option to see the LaTeX output.

Parsing line-based structure (ray tracer) without using too many vars

I want to parse a file in scala (probably using JavaTokerParsers?). Possibly without using too many vars :-)
The file is the input for a ray tracer.
It is a line based file structure.
Three types of lines exists: empty line, comment line and command line
The comment line starts with # (maybe has some whitespace before the #)
Command line starts with an identifier optionally followed by a number of parameters (float or filename).
How would I go about this. I would want to parser to be called like this
val scene = parseAll(sceneFile, file);
Sample file:
#Cornell Box
size 640 480
camera 0 0 1 0 0 -1 0 1 0 45
output scene6.png
maxdepth 5
maxverts 12
#planar face
vertex -1 +1 0
vertex -1 -1 0
vertex +1 -1 0
vertex +1 +1 0
#cube
vertex -1 +1 +1
vertex +1 +1 +1
vertex -1 -1 +1
vertex +1 -1 +1
vertex -1 +1 -1
vertex +1 +1 -1
vertex -1 -1 -1
vertex +1 -1 -1
ambient 0 0 0
specular 0 0 0
shininess 1
emission 0 0 0
diffuse 0 0 0
attenuation 1 0.1 0.05
point 0 0.44 -1.5 0.8 0.8 0.8
directional 0 1 -1 0.2 0.2 0.2
diffuse 0 0 1
#sphere 0 0.8 -1.5 0.1
pushTransform
#red
pushTransform
translate 0 0 -3
rotate 0 1 0 60
scale 10 10 1
diffuse 1 0 0
tri 0 1 2
tri 0 2 3
popTransform
#green
pushTransform
translate 0 0 -3
rotate 0 1 0 -60
scale 10 10 1
diffuse 0 1 0
tri 0 1 2
tri 0 2 3
popTransform
#back
pushTransform
scale 10 10 1
translate 0 0 -2
diffuse 1 1 1
tri 0 1 2
tri 0 2 3
popTransform
#sphere
diffuse 0.7 0.5 0.2
specular 0.2 0.2 0.2
pushTransform
translate 0 -0.7 -1.5
scale 0.1 0.1 0.1
sphere 0 0 0 1
popTransform
#cube
diffuse 0.5 0.7 0.2
specular 0.2 0.2 0.2
pushTransform
translate -0.25 -0.4 -1.8
rotate 0 1 0 15
scale 0.25 0.4 0.2
diffuse 1 1 1
tri 4 6 5
tri 6 7 5
tri 4 5 8
tri 5 9 8
tri 7 9 5
tri 7 11 9
tri 4 8 10
tri 4 10 6
tri 6 10 11
tri 6 11 7
tri 10 8 9
tri 10 9 11
popTransform
popTransform
popTransform
Maybe I've pushed it too hard for the one liner but that's my take (although idiomatic it might not be optimal):
First, CommandParams represents a command along with its arguments in a list format. If no arguments then we have None args:
case class CommandParams(command:String, params:Option[List[String]])
Then here's the file parsing and construction one liner along with line-by-line explanation:
val fileToDataStructure = Source.fromFile("file.txt").getLines() //open file and get lines iterator
.filter(!_.isEmpty) //exclude empty lines
.filter(!_.startsWith("#")) //exclude comments
.foldLeft(List[CommandParams]()) //iterate and store in a list of CommandParams
{(listCmds:List[CommandParams], line:String) => //tuple of a list of objs so far and the current line
val arr = line.split("\\s") //split line on any space delim
val command = arr.head //first element of array is the command
val args = if(arr.tail.isEmpty) None else Option(arr.tail.toList) //rest are their params
new CommandParams(command, args)::listCmds //construct the obj and cons it to the list
}
.reverse //due to cons concat we need to reverse to preserve order
A demo output iterating through it:
fileToDataStructure.foreach(println)
yields:
CommandParams(size,Some(List(640, 480)))
CommandParams(camera,Some(List(0, 0, 1, 0, 0, -1, 0, 1, 0, 45)))
CommandParams(output,Some(List(scene6.png)))
CommandParams(maxdepth,Some(List(5)))
CommandParams(maxverts,Some(List(12)))
CommandParams(vertex,Some(List(-1, +1, 0)))
...
CommandParams(pushTransform,None)
CommandParams(pushTransform,None)
CommandParams(translate,Some(List(0, 0, -3)))
...
A demo of how to iterate through it to do actual work once loaded:
fileToDataStructure.foreach{
cmdParms => cmdParms match {
case CommandParams(cmd, None) => println(s"I'm a ${cmd} with no args")
case CommandParams(cmd, Some(args))=> println(s"I'm a ${cmd} with args: ${args.mkString(",")}")
}
}
yields output:
I'm a size with args: 640,480
I'm a camera with args: 0,0,1,0,0,-1,0,1,0,45
I'm a output with args: scene6.png
I'm a maxdepth with args: 5
I'm a maxverts with args: 12
I'm a vertex with args: -1,+1,0
...
I'm a popTransform with no args
I'm a popTransform with no args

problem with using statement in gnuplot

I'm trying to plot some data. I have a file looking like this
50 11 1 1
100 29 1 6
200 62 4 26
300 104 9 39
and a plotfile
# Vergleich
set terminal png
set output "streetsegments.png"
set datafile separator "\t"
set yrange [0:105]
set ytics ("0" 0, "10" 10, "20" 20, "30" 30, "40" 40, "50" 50, "75" 75, "100" 100)
set xtics ("50" 50, "100" 100, "200" 200,"300" 300)
set xrange [0:300]
set xlabel "Error in meters"
set ylabel "Street segments"
set notitle
plot "segments" using 1:2 t 'foo' with lp, \
"segments" u 1:3 t 'bar' with lp, \
"segments" u 1:4 t 'baz' with lp
But when plotting, it doesn't show anything besides the titles of the lines, but no lines themselves are drawn. Any idea, why this doesn't work?
When only plotting on column (by not using the "using"-statement ) it works just fine.
Works for me. Try again removing the set datafile separator "\t" line and see if that solves it.

Resources