Page4
vegalite
Year Chart
Year Chart
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"title": "Potsdam/GWL, 1991-2020",
"data": {
"url": "includes/gwl.csv"
},
"vconcat": [
{
"encoding": {
"color": {
"condition": {
"param": "brush",
"title": "Patterns",
"field": "gwl",
"type": "nominal",
"scale": {
"domain": ["W","SW","NW","TRM","S","NE","N","HN","HF","HM","BM","HB","U","SE","TB","HNF","TRW"],
"range": ["green","red","blue","magenta","red","blue","blue","yellow","yellow","orange","orange","yellow","gray","red","green","yellow","red"]
}
},
"value": "lightgray"
},
"size": {
"title": "Precipitation",
"field": "nied",
"scale": {"domain": [-1, 40]},
"type": "quantitative"
},
"x": {
"field": "date",
"timeUnit": "yeardate",
"title": null,
"axis": {"format": "%y"}
},
"y": {
"title": "Maximum Daily Temperature (°C)",
"field": "tmax",
"scale": {"domain": [-15, 40]},
"type": "quantitative"
}
},
"width": 1000,
"height": 500,
"mark": "point",
"params": [{
"name": "brush",
"select": {"type": "interval", "encodings": ["x"]}
}],
"transform": [{"filter": {"param": "click"}}]
},
{
"encoding": {
"color": {
"condition": {
"param": "click",
"field": "gwl",
"scale": {
"domain": ["W","SW","NW","TRM","S","NE","N","HN","HF","HM","BM","HB","U","SE","TB","HNF","TRW"],
"range": ["green","red","blue","magenta","red","blue","blue","yellow","yellow","orange","orange","yellow","gray","red","green","yellow","red"] }
},
"value": "lightgray"
},
"x": {"aggregate": "count"},
"y": {"title": null, "field": "gwl"}
},
"width": 1000,
"mark": "bar",
"params": [{
"name": "click",
"select": {"type": "point", "encodings": ["color"]}
}],
"transform": [{"filter": {"param": "brush"}}]
}
]
}
Year Chart
Year Chart
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"title": "Potsdam/Year, 1991-2020",
"data": {
"url": "includes/gwl.csv"
},
"vconcat": [
{
"encoding": {
"color": {
"condition": {
"param": "brush",
"field": "year",
"type": "nominal",
"scale": {
"domain": ["1961-1990","1991-2000","2001-2010","2011-2020"],
"range": ["gray","yellow","orange","red"]
}
},
"value": "lightgray"
},
"size": {
"title": "Precipitation",
"field": "nied",
"scale": {"domain": [-1, 40]},
"type": "quantitative"
},
"x": {
"field": "date",
"timeUnit": "monthdate",
"title": null,
"axis": {"format": "%b"}
},
"y": {
"title": "Maximum Daily Temperature (°C)",
"field": "tmax",
"scale": {"domain": [-15, 40]},
"type": "quantitative"
}
},
"width": 1000,
"height": 400,
"mark": "point",
"params": [{
"name": "brush",
"select": {"type": "interval", "encodings": ["x"]}
}],
"transform": [{"filter": {"param": "click"}}]
},
{
"encoding": {
"color": {
"condition": {
"param": "click",
"field": "year",
"scale": {
"domain": ["1961-1990","1991-2000","2001-2010","2011-2020"],
"range": ["gray","yellow","orange","red"]
}
},
"value": "lightgray"
},
"x": {"aggregate": "count"},
"y": {"title": null, "field": "year"}
},
"width": 1000,
"height": 200,
"mark": "bar",
"params": [{
"name": "click",
"select": {"type": "point", "encodings": ["color"]}
}],
"transform": [{"filter": {"param": "brush"}}]
}
]
}
World Map
World Map
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A plot of 2016 annual global precipitation data from the NOAA Climate Forecast System (CFS) v2.",
"width": 1000,
"height": 400,
"autosize": "fit-x",
"signals": [
{
"name": "projection", "value": "naturalEarth1",
"bind": {"input": "select", "options": [
"azimuthalEqualArea", "equalEarth", "equirectangular",
"naturalEarth1", "mollweide", "orthographic"
]}
},
{
"name": "scale", "value": 110,
"bind": {"input": "range", "min": 50, "max": 400, "step": 5}
},
{
"name": "rotate0", "value": 0,
"bind": {"input": "range", "min": -180, "max": 180, "step": 1}
},
{
"name": "rotate1", "value": 0,
"bind": {"input": "range", "min": 0, "max": 360, "step": 1}
},
{
"name": "rotate2", "value": 0,
"bind": {"input": "range", "min": 0, "max": 360, "step": 1}
},
{
"name": "opacity", "value": 0.5,
"bind": {"input": "range", "min": 0, "max": 1, "step": 0.01}
},
{
"name": "levels", "value": 6,
"bind": {"input": "range", "min": 2, "max": 12, "step": 1}
},
{
"name": "stop", "value": 3000
},
{
"name": "step",
"update": "stop / levels"
}
],
"data": [
{
"name": "precipitation",
"url": "includes/annual-precip.json"
},
{
"name": "contours",
"source": "precipitation",
"transform": [
{
"type": "isocontour",
"thresholds": {"signal": "sequence(step, stop, step)"}
}
]
},
{
"name": "world",
"url": "includes/world-110m.json",
"format": {"type": "topojson", "feature": "countries"}
}
],
"projections": [
{
"name": "projection",
"type": {"signal": "projection"},
"scale": {"signal": "scale"},
"rotate": {"signal": "[rotate0, rotate1, rotate2]"},
"translate": {"signal": "[width/2, height/2]"}
}
],
"scales": [
{
"name": "color",
"type": "quantize",
"domain": {"signal": "[0, stop]"},
"range": {"scheme": "bluepurple", "count": {"signal": "levels"}}
}
],
"marks": [
{
"type": "shape",
"clip": true,
"from": {"data": "world"},
"encode": {
"update": {
"strokeWidth": {"value": 1},
"stroke": {"value": "#eee"},
"fill": {"value": "#ddd"}
}
},
"transform": [
{
"type": "geoshape",
"projection": "projection"
}
]
},
{
"type": "shape",
"clip": true,
"from": { "data": "contours"},
"encode": {
"update": {
"fill": {"scale": "color", "field": "contour.value"},
"fillOpacity": {"signal": "opacity"}
}
},
"transform": [
{
"type": "geoshape",
"field": "datum.contour",
"projection": "projection"
}
]
}
],
"legends": [
{
"title": "Annual Precipitation (mm)",
"fill": "color",
"orient": "bottom",
"offset": 5,
"type": "gradient",
"gradientLength": 300,
"gradientThickness": 12,
"titlePadding": 10,
"titleOrient": "left",
"titleAnchor": "end",
"direction": "horizontal"
}
],
"config": {
"legend": {"layout": {"anchor": "middle"}}
}
}
Contour
Contour
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A contour plot of the Maungawhau volcano in New Zealand.",
"width": 1000,
"height": 300,
"signals": [
{
"name": "grid",
"init": "data('volcano')[0]"
},
{
"name": "height",
"update": "round(grid.height * width / grid.width)"
},
{
"name": "smooth", "value": true,
"bind": {"input": "radio", "options": [true, false]}
}
],
"data": [
{
"name": "volcano",
"url": "includes/volcano.json"
},
{
"name": "contours",
"source": "volcano",
"transform": [
{
"type": "isocontour",
"scale": {"expr": "width / datum.width"},
"smooth": {"signal": "smooth"},
"thresholds": {"signal": "sequence(90, 195, 5)"}
}
]
}
],
"scales": [
{
"name": "color",
"type": "linear",
"domain": [90, 190],
"range": {"scheme": "blueorange"}
}
],
"marks": [
{
"type": "path",
"from": {"data": "contours"},
"encode": {
"enter": {
"stroke": {"value": "#ccc"},
"strokeWidth": {"value": 1},
"fill": {"scale": "color", "field": "contour.value"}
}
},
"transform": [
{
"type": "geopath",
"field": "datum.contour"
}
]
}
]
}
Time Series
Time Series
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "Plot showing a 30 day rolling average with raw values in the background.",
"height": 300,
"data": {"url": "includes/seattle-weather.csv"},
"transform": [{
"window": [
{
"field": "temp_max",
"op": "mean",
"as": "rolling_mean"
}
],
"frame": [-15, 15]
}],
"encoding": {
"x": {"field": "date", "type": "temporal", "title": "Date","title": null},
"y": {"type": "quantitative", "axis": {"title": "Max Temperature and Rolling Mean"}}
},
"layer": [
{
"mark": {"type": "point", "opacity": 0.3},
"encoding": {
"y": {"field": "temp_max", "title": "Max Temperature"}
}
},
{
"mark": {"type": "line", "color": "red", "size": 3},
"encoding": {
"y": {"field": "rolling_mean", "title": "Rolling Mean of Max Temperature"}
}
}
]
}
Climate Diagram
Climate Diagram
{
"$schema": "https://vega.github.io/schema/vega-lite/v2.json",
"height": 400,
"data": {"url": "includes/gwl.csv"},
"layer": [
{
"mark": "bar",
"encoding": {
"x": {
"timeUnit": "month",
"field": "date",
"type": "ordinal"
},
"y": {
"aggregate": "mean",
"field": "nied",
"title": "Precipitation",
"type": "quantitative",
"axis": {
"grid": false
}
},
"color": {"value": "dodgerblue"}
}
},
{
"mark": {"type": "line","strokeWidth": 5},
"encoding": {
"x": {
"timeUnit": "month",
"field": "date",
"title": null,
"type": "ordinal"
},
"y": {
"aggregate": "mean",
"field": "tmax",
"type": "quantitative",
"axis": {
"grid": false,
"title": "Temperature"
},
"scale": {"zero": false}
},
"color": {"value": "orangered"}
}
}
],
"resolve": {"scale": {"y": "independent"}}
}
Scatter
GWL
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"width": 1000,
"height": 500,
"padding": {"left": 5, "right": 5, "top": 20, "bottom": 0},
"autosize": "none",
"data": [
{
"name": "table",
"url": "includes/gwl.json",
"format": {"type": "json"}
}
],
"signals": [
{"name": "cx", "update": "width / 2"},
{"name": "cy", "update": "height / 2"},
{
"name": "gravityX",
"value": 0.2,
"bind": {"input": "range", "min": 0, "max": 1}
},
{
"name": "gravityY",
"value": 0.1,
"bind": {"input": "range", "min": 0, "max": 1}
}
],
"scales": [
{
"name": "size",
"domain": {"data": "table", "field": "amount"},
"range": [100, 3000]
},
{
"name": "color",
"type": "ordinal",
"domain": {"data": "table", "field": "category"},
"range": "ramp"
}
],
"marks": [
{
"name": "nodes",
"type": "symbol",
"from": {"data": "table"},
"encode": {
"enter": {
"fill": {"scale": "color", "field": "category"},
"xfocus": {"signal": "cx"},
"yfocus": {"signal": "cy"}
},
"update": {
"size": {"signal": "pow(2 * datum.amount, 2)", "scale": "size"},
"stroke": {"value": "white"},
"strokeWidth": {"value": 1},
"tooltip": {"signal": "datum"}
}
},
"transform": [
{
"type": "force",
"iterations": 100,
"static": false,
"forces": [
{
"force": "collide",
"iterations": 2,
"radius": {"expr": "sqrt(datum.size) / 2"}
},
{"force": "center", "x": {"signal": "cx"}, "y": {"signal": "cy"}},
{"force": "x", "x": "xfocus", "strength": {"signal": "gravityX"}},
{"force": "y", "y": "yfocus", "strength": {"signal": "gravityY"}}
]
}
]
},
{
"type": "text",
"from": {"data": "nodes"},
"encode": {
"enter": {
"align": {"value": "center"},
"baseline": {"value": "middle"},
"fontSize": {"value": 15},
"fontWeight": {"value": "bold"},
"fill": {"value": "white"},
"text": {"field": "datum.category"}
},
"update": {"x": {"field": "x"}, "y": {"field": "y"}}
}
}
]
}
Parallel
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "Parallel coordinates plot showing 7 dimensions of automobile statistics.",
"width": 1000,
"height": 400,
"padding": 5,
"config": {
"axisY": {
"titleX": -2,
"titleY": 410,
"titleAngle": 0,
"titleAlign": "right",
"titleBaseline": "top"
}
},
"data": [
{
"name": "cars",
"url": "includes/cars.json",
"format": {
"type": "json",
"parse": {"Year": "date:%Y-%m-%d"}
},
"transform": [
{ "type": "filter", "expr": "datum.Horsepower && datum.Miles_per_Gallon" },
{ "type": "formula", "as": "Year",
"expr": "isNumber(datum.year) ? datum.year : year(datum.Year)" }
]
},
{
"name": "fields",
"values": [
"Cylinders",
"Displacement",
"Weight_in_lbs",
"Horsepower",
"Acceleration",
"Miles_per_Gallon",
"Year"
]
}
],
"scales": [
{
"name": "ord", "type": "point",
"range": "width", "round": true,
"domain": {"data": "fields", "field": "data"}
},
{
"name": "Cylinders", "type": "linear",
"range": "height", "zero": false, "nice": true,
"domain": {"data": "cars", "field": "Cylinders"}
},
{
"name": "Displacement", "type": "linear",
"range": "height", "zero": false, "nice": true,
"domain": {"data": "cars", "field": "Displacement"}
},
{
"name": "Weight_in_lbs", "type": "linear",
"range": "height", "zero": false, "nice": true,
"domain": {"data": "cars", "field": "Weight_in_lbs"}
},
{
"name": "Horsepower", "type": "linear",
"range": "height", "zero": false, "nice": true,
"domain": {"data": "cars", "field": "Horsepower"}
},
{
"name": "Acceleration", "type": "linear",
"range": "height", "zero": false, "nice": true,
"domain": {"data": "cars", "field": "Acceleration"}
},
{
"name": "Miles_per_Gallon", "type": "linear",
"range": "height", "zero": false, "nice": true,
"domain": {"data": "cars", "field": "Miles_per_Gallon"}
},
{
"name": "Year", "type": "linear",
"range": "height", "zero": false, "nice": true,
"domain": {"data": "cars", "field": "Year"}
}
],
"axes": [
{
"orient": "left", "zindex": 1,
"scale": "Cylinders", "title": "Cylinders",
"offset": {"scale": "ord", "value": "Cylinders", "mult": -1}
},
{
"orient": "left", "zindex": 1,
"scale": "Displacement", "title": "Displacement",
"offset": {"scale": "ord", "value": "Displacement", "mult": -1}
},
{
"orient": "left", "zindex": 1,
"scale": "Weight_in_lbs", "title": "Weight_in_lbs",
"offset": {"scale": "ord", "value": "Weight_in_lbs", "mult": -1}
},
{
"orient": "left", "zindex": 1,
"scale": "Horsepower", "title": "Horsepower",
"offset": {"scale": "ord", "value": "Horsepower", "mult": -1}
},
{
"orient": "left", "zindex": 1,
"scale": "Acceleration", "title": "Acceleration",
"offset": {"scale": "ord", "value": "Acceleration", "mult": -1}
},
{
"orient": "left", "zindex": 1,
"scale": "Miles_per_Gallon", "title": "Miles_per_Gallon",
"offset": {"scale": "ord", "value": "Miles_per_Gallon", "mult": -1}
},
{
"orient": "left", "zindex": 1,
"scale": "Year", "title": "Year", "format": "d",
"offset": {"scale": "ord", "value": "Year", "mult": -1}
}
],
"marks": [
{
"type": "group",
"from": {"data": "cars"},
"marks": [
{
"type": "line",
"from": {"data": "fields"},
"encode": {
"enter": {
"x": {"scale": "ord", "field": "data"},
"y": {
"scale": {"datum": "data"},
"field": {"parent": {"datum": "data"}}
},
"stroke": {"value": "steelblue"},
"strokeWidth": {"value": 1.01},
"strokeOpacity": {"value": 0.3}
}
}
}
]
}
]
}
Lines
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A basic line chart example.",
"width": 500,
"height": 200,
"padding": 5,
"signals": [
{
"name": "interpolate",
"value": "linear",
"bind": {
"input": "select",
"options": [
"basis",
"cardinal",
"catmull-rom",
"linear",
"monotone",
"natural",
"step",
"step-after",
"step-before"
]
}
}
],
"data": [
{
"name": "table",
"values": [
{"x": 0, "y": 28, "c":0}, {"x": 0, "y": 20, "c":1},
{"x": 1, "y": 43, "c":0}, {"x": 1, "y": 35, "c":1},
{"x": 2, "y": 81, "c":0}, {"x": 2, "y": 10, "c":1},
{"x": 3, "y": 19, "c":0}, {"x": 3, "y": 15, "c":1},
{"x": 4, "y": 52, "c":0}, {"x": 4, "y": 48, "c":1},
{"x": 5, "y": 24, "c":0}, {"x": 5, "y": 28, "c":1},
{"x": 6, "y": 87, "c":0}, {"x": 6, "y": 66, "c":1},
{"x": 7, "y": 17, "c":0}, {"x": 7, "y": 27, "c":1},
{"x": 8, "y": 68, "c":0}, {"x": 8, "y": 16, "c":1},
{"x": 9, "y": 49, "c":0}, {"x": 9, "y": 25, "c":1}
]
}
],
"scales": [
{
"name": "x",
"type": "point",
"range": "width",
"domain": {"data": "table", "field": "x"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true,
"zero": true,
"domain": {"data": "table", "field": "y"}
},
{
"name": "color",
"type": "ordinal",
"range": "category",
"domain": {"data": "table", "field": "c"}
}
],
"axes": [
{"orient": "bottom", "scale": "x"},
{"orient": "left", "scale": "y"}
],
"marks": [
{
"type": "group",
"from": {
"facet": {
"name": "series",
"data": "table",
"groupby": "c"
}
},
"marks": [
{
"type": "line",
"from": {"data": "series"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "x"},
"y": {"scale": "y", "field": "y"},
"stroke": {"scale": "color", "field": "c"},
"strokeWidth": {"value": 2}
},
"update": {
"interpolate": {"signal": "interpolate"},
"strokeOpacity": {"value": 1}
},
"hover": {
"strokeOpacity": {"value": 0.5}
}
}
}
]
}
]
}
Code
Importing
Reading
file = '/home/peterh/Documents/DATA/dat/03987.dat'
dat = N.genfromtxt(file,names=True,dtype=None)
file = '/home/peterh/Documents/DATA/dat/gwlneudatum.dat'
gwl = N.genfromtxt(file,names=True,dtype=None)
Writing
id = N.where((dat['jahr']>=1961)&(dat['jahr']<=2020))[0]
ta = dat['ta'][id]
mo = dat['mo'][id]
ja = dat['jahr'][id]
tx = dat['tmax'][id]
pr = dat['nied'][id]
id = N.where((dat['jahr']>=1961)&(dat['jahr']<=2020))[0]
gw = gwl['gw'][id]
gw = N.array(gw,str)
gw[gw=='SWZ'] = 'SW'
gw[gw=='SWA'] = 'SW'
gw[gw=='SEZ'] = 'SE'
gw[gw=='SEA'] = 'SE'
gw[gw=='NWZ'] = 'NW'
gw[gw=='NWA'] = 'NW'
gw[gw=='TM'] = 'TRM'
gw[gw=='SZ'] = 'S'
gw[gw=='SA'] = 'S'
gw[gw=='NEZ'] = 'NE'
gw[gw=='NEA'] = 'NE'
gw[gw=='NZ'] = 'N'
gw[gw=='NA'] = 'N'
gw[gw=='WZ'] = 'W'
gw[gw=='WA'] = 'W'
gw[gw=='WW'] = 'W'
gw[gw=='WS'] = 'W'
gw[gw=='HNZ'] = 'HN'
gw[gw=='HNA'] = 'HN'
gw[gw=='HFZ'] = 'HF'
gw[gw=='HFA'] = 'HF'
gw[gw=='HNFZ'] = 'HNF'
gw[gw=='HNFA'] = 'HNF'
nd = len(gw)
f = open('includes/gwl.csv','w')
f.write('date,tmax,nied,gwl,year\n')
for d in range(nd):
if((ja[d]>=1961)&(ja[d]<=1990)): dec = '1961-1990'
if((ja[d]>=1991)&(ja[d]<=2000)): dec = '1991-2000'
if((ja[d]>=2001)&(ja[d]<=2010)): dec = '2001-2010'
if((ja[d]>=2011)&(ja[d]<=2020)): dec = '2011-2020'
f.write('%04i-%02i-%02i,%.1f,%.1f,%s,%s\n'%(ja[d],mo[d],ta[d],tx[d],pr[d],gw[d],dec))
f.close()
json
f = open('./page4/includes/gwl.json','w')
f.write('[\n')
go = list(set(gw))
for g in go:
id = N.where((pr>=20)&(gw==g))[0]
ii = N.where((gw==g))[0]
f.write('\t{\n')
f.write('\t\t"category": "%s",\n'%g)
f.write('\t\t"amount": %.5f\n'%(100.*len(id)/len(ii)))
f.write('\t},\n')
f.write(']\n')
f.close()