# Wolfram Language™

Open live version

## Plot the Yearly Path of the Sun

Make a webpage that shows the yearly path of the Sun at the visitors location.

### code

SunPlot[] := Block[{points, xMin, xMax, yMin, yMax, skyTopColor = Hue[0.6, 0.7, 0.5], skyBottomColor = Hue[0.6, 0.5, 0.9], groundColor = Hue[0, 0.5, 0.2], sunColor = Hue[0.15, 0.3, 0.9]}, points = Map[QuantityMagnitude, SunPosition[DateRange[ DateObject[{2014, 1, 1, 9, 0}], DateObject[{2014, 12, 31, 9, 0}], 10]]["Values"], {2}]; {{xMin, xMax}, {yMin, yMax}} = ({Min[#], Max[#]} + 10 {-1, 1}) & /@ Transpose[points]; yMin = -5; Graphics[{ {Blue, Polygon[{{xMin, yMin}, {xMin, yMax}, {xMax, yMax}, {xMax, yMin}}, VertexColors -> {skyBottomColor, skyTopColor, skyTopColor, skyBottomColor}]}, {groundColor, Rectangle[{xMin, yMin}, {xMax, 0}]}, {sunColor, Disk[#, .5] & /@ points} }, Frame -> True, ImageSize -> 500, PlotLabel -> "Yearly path of the Sun at 9:00 in " <> CommonName[First[GeoIdentify["City", \$GeoLocation]]]] ]
CloudDeploy[ APIFunction[{}, Pane[SunPlot[], ImageMargins -> 100] &, "PNG"], Permissions -> "Public"]

### how it works

The Suns analemma, or yearly path in the sky, is a set of snapshots of the position of the Sun every day at the same time of day. This expression returns the {azimuth, altitude} coordinates of the Suns position in 10-day increments throughout one year:

positions = SunPosition[ DateRange[DateObject[{2014, 1, 1, 9, 0}], DateObject[{2014, 12, 31, 9, 0}], 10]]["Values"]

Extract numeric values by mapping QuantityMagnitude at level 2 of that list:

points = Map[QuantityMagnitude, positions, {2}]

From those points, make a graphic showing the Suns path throughout the year:

Graphics[Point[points], Frame -> True]

Format the graphic to make it more attractive and more suggestive of a view of the sky above the horizon. First find the minima and maxima of the azimuth and altitudes of the Suns path, and add 10° in each direction to widen the view slightly. Replace yMin by -5° so that the view extends 5° below the horizon:

{{xMin, xMax}, {yMin, yMax}} = ({Min[#], Max[#]} + 10 {-1, 1}) & /@ Transpose[points]; yMin = -5;

The Sun subtends about 0.5° in the sky. Give the Sun images the appropriate radii using Disk rather than Point, and specify the PlotRange necessary to give a wider view:

Graphics[Disk[#, 0.5] & /@ points, Frame -> True, PlotRange -> {{xMin, xMax}, {yMin, yMax}}]

Add graphic elements to render the ground and horizon, and the sky shaded from light blue at the bottom to dark blue at the top. Add a plot label that specifies the time and location of the image. To put a color setter inline in the code, type DynamicSetting[ColorSetter[]], select that expression, and press Shift-Return to evaluate it in place. Click the resulting color setter and pick a color:

SunPlot[] := Block[{points, xMin, xMax, yMin, yMax, skyTopColor = \!\(\* TagBox[ ColorSetterBox[Hue[0.6, 0.7, 0.5], "Swatch"], Setting]\), skyBottomColor = \!\(\* TagBox[ ColorSetterBox[Hue[0.6, 0.5, 0.9], "Swatch"], Setting]\), groundColor = \!\(\* TagBox[ ColorSetterBox[Hue[0, 0.5, 0.2], "Swatch"], Setting]\), sunColor = \!\(\* TagBox[ ColorSetterBox[Hue[0.15, 0.3, 0.9], "Swatch"], Setting]\)}, points = Map[QuantityMagnitude, SunPosition[DateRange[ DateObject[{2014, 1, 1, 9, 0}], DateObject[{2014, 12, 31, 9, 0}], 10]]["Values"], {2}]; {{xMin, xMax}, {yMin, yMax}} = ({Min[#], Max[#]} + 10 {-1, 1}) & /@ Transpose[points]; yMin = -5; Graphics[{ {Blue, Polygon[{{xMin, yMin}, {xMin, yMax}, {xMax, yMax}, {xMax, yMin}}, VertexColors -> {skyBottomColor, skyTopColor, skyTopColor, skyBottomColor}]}, {groundColor, Rectangle[{xMin, yMin}, {xMax, 0}]}, {sunColor, Disk[#, .5] & /@ points} }, Frame -> True, ImageSize -> 500, PlotLabel -> "Yearly path of the Sun at 9:00 in " <> CommonName[First[GeoIdentify["City", \$GeoLocation]]]] ]
SunPlot[]

To make the image available to everyone, deploy it to the Wolfram Cloud, adding margins around the image using Pane.

obj = CloudDeploy[ APIFunction[{}, Pane[SunPlot[], ImageMargins -> 100] &, "PNG"], Permissions -> "Public"]

The image each visitor sees when visiting the deployed image is customized to their location.