Sun path

46 views
Skip to first unread message

Mark Mikofski

unread,
Dec 29, 2019, 12:22:59 AM12/29/19
to pvlib-python
Hi Ashwin,

Given latitude, longitude, and a time series you can use `pvlib.solarposition.get_solarposition(times, latitude, longitude)`


This returns a dataframe with the azimuth and zenith for times in the time series. You can use this to plot a sun path like the one in the stack exchange post using something like `pyplot.polar(theta, r)` from Matplotlib:


For example:

import matplotlib.pyplot as plt
from pvlib import solarposition
import pandas as pd

plt.ion()

times = pd.DateTimeIndex(pd.date_range('2019-01-01 00:00:00', '2019-12-31 23:59:59', freq='H', tz='Etc/GMT+8'))


solpos = solarposition.get_solarposition(
    times, 35.8, -122.1)
plt.polar(solpos.azimuth, solpos.apparent_zenith)

You can get fancy by plotting seasons, months or days separately

Forgive any errors, I wrote this without testing on my phone, sorry.

Good luck!


From Ashwin D.:

I am wanting to make sun path diagrams as shown in this top voted Physics SE answer - https://physics.stackexchange.com/questions/479515/why-are-solar-panels-kept-tilted/479592#479592

Is that possible with pv lib python ?

Debayan Paul

unread,
Jan 3, 2020, 6:05:52 AM1/3/20
to pvlib-python
Hi Mark, 

Is it possible to make the Sun Path diagram for the entire year with hourly frequency and solstices marked separately? I ended up with this diagram with your code.

Sunpath.png

I was looking for almost similar diagram Ashwin was asking for.

Thanks,
Debayan

cwh...@sandia.gov

unread,
Jan 3, 2020, 10:41:17 AM1/3/20
to pvlib-python
pyplot.polar expects angles in radians, whereas pvlib provides azimuth in degrees. Also, you should only plot points with positive elevation (zenith < 90).

I get this. I suppose one can work with the axis labeling and options to rotate to azimuth rather than angles.

Cheers,

Cliff


sunpath.png

Kevin Anderson

unread,
Jan 3, 2020, 10:58:29 AM1/3/20
to pvlib-python
Here's a full example:
import matplotlib.pyplot as plt
from pvlib import solarposition
import pandas as pd
import numpy as np

plt.ion()
tz = 'Asia/Calcutta'
times = pd.date_range('2019-01-01 00:00:00', '2020-01-01', closed='left',
                      freq='H', tz=tz)

solpos = solarposition.get_solarposition(times, 28.6, 77.2)

# remove nighttime
solpos = solpos.loc[solpos['apparent_elevation'] > 0, :]

# draw the analemma loops
ax = plt.subplot(1, 1, 1, projection='polar')
ax.scatter(np.radians(solpos.azimuth), solpos.apparent_zenith, s=2)

# draw hour labels
for hour in np.unique(solpos.index.hour):
    # choose label position by the smallest radius for each hour
    subset = solpos.loc[solpos.index.hour == hour, :]
    r = subset.apparent_zenith
    pos = solpos.loc[r.idxmin(), :]
    ax.text(np.radians(pos['azimuth']), pos['apparent_zenith'], str(hour))

# draw individual days
for date in pd.to_datetime(['2019-03-21', '2019-06-21', '2019-12-21']):
    times = pd.date_range(date, date+pd.Timedelta('24h'), freq='5min', tz=tz)
    solpos = solarposition.get_solarposition(times, 28.6, 77.2)
    solpos = solpos.loc[solpos['apparent_elevation'] > 0, :]
    label = date.strftime('%Y-%m-%d')
    ax.plot(np.radians(solpos.azimuth), solpos.apparent_zenith, label=label)

ax.figure.legend(loc='upper right')

# change coordinates to be like a compass
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_rmax(90)
 
Kevin

William Holmgren

unread,
Jan 3, 2020, 12:44:42 PM1/3/20
to Kevin Anderson, pvlib-python
Nice! This would be a great example to add to the documentation.

Will

--
You received this message because you are subscribed to the Google Groups "pvlib-python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pvlib-python...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pvlib-python/d2d37d3c-f6d4-48fb-a65e-1ec7e8510761%40googlegroups.com.

Debayan Paul

unread,
Jan 3, 2020, 1:28:45 PM1/3/20
to pvlib-python
Brilliant.

Thanks, Kevin. Exactly I was looking for this.

Cheers,
Debayan

On Sunday, December 29, 2019 at 6:22:59 AM UTC+1, Mark Mikofski wrote:

Mark Mikofski

unread,
Jan 3, 2020, 3:30:24 PM1/3/20
to pvlib-python
I second that! This is great! Please add to examples, thx!
Reply all
Reply to author
Forward
0 new messages