Skip to content

Commit 3d778ea

Browse files
authored
Update colorbar tick names in sunpath gallery example (#2097)
* Update plot_sunpath_diagrams.py * Update legend location * Update xticks and ylim * Update v0.11.0.rst * Code review by Ioannis * More formatting updates
1 parent d21c1b3 commit 3d778ea

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

docs/examples/solar-position/plot_sunpath_diagrams.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,32 @@
3232
ax = plt.subplot(1, 1, 1, projection='polar')
3333
# draw the analemma loops
3434
points = ax.scatter(np.radians(solpos.azimuth), solpos.apparent_zenith,
35-
s=2, label=None, c=solpos.index.dayofyear)
36-
ax.figure.colorbar(points)
35+
s=2, label=None, c=solpos.index.dayofyear,
36+
cmap='twilight_shifted_r')
37+
# add and format colorbar
38+
cbar = ax.figure.colorbar(points)
39+
times_ticks = pd.date_range('2019-01-01', '2020-01-01', freq='MS', tz=tz)
40+
cbar.set_ticks(ticks=times_ticks.dayofyear, labels=[], minor=False)
41+
cbar.set_ticks(ticks=times_ticks.dayofyear+15,
42+
labels=times_ticks.strftime('%b'),
43+
minor=True)
44+
cbar.ax.tick_params(which='minor', width=0)
3745

3846
# draw hour labels
3947
for hour in np.unique(solpos.index.hour):
4048
# choose label position by the smallest radius for each hour
4149
subset = solpos.loc[solpos.index.hour == hour, :]
4250
r = subset.apparent_zenith
4351
pos = solpos.loc[r.idxmin(), :]
44-
ax.text(np.radians(pos['azimuth']), pos['apparent_zenith'], str(hour))
52+
ax.text(np.radians(pos['azimuth']), pos['apparent_zenith'],
53+
str(hour).zfill(2), ha='center', va='bottom')
4554

4655
# draw individual days
4756
for date in pd.to_datetime(['2019-03-21', '2019-06-21', '2019-12-21']):
4857
times = pd.date_range(date, date+pd.Timedelta('24h'), freq='5min', tz=tz)
4958
solpos = solarposition.get_solarposition(times, lat, lon)
5059
solpos = solpos.loc[solpos['apparent_elevation'] > 0, :]
51-
label = date.strftime('%Y-%m-%d')
60+
label = date.strftime('%b %d')
5261
ax.plot(np.radians(solpos.azimuth), solpos.apparent_zenith, label=label)
5362

5463
ax.figure.legend(loc='upper left')
@@ -64,7 +73,7 @@
6473
# This is a polar plot of hourly solar zenith and azimuth. The figure-8
6574
# patterns are called `analemmas <https://en.wikipedia.org/wiki/Analemma>`_ and
6675
# show how the sun's path slowly shifts over the course of the year . The
67-
# colored lines show the single-day sun paths for the winter and summer
76+
# solid colored lines show the single-day sun paths for the winter and summer
6877
# solstices as well as the spring equinox.
6978
#
7079
# The soltice paths mark the boundary of the sky area that the sun traverses
@@ -111,25 +120,37 @@
111120

112121
fig, ax = plt.subplots()
113122
points = ax.scatter(solpos.azimuth, solpos.apparent_elevation, s=2,
114-
c=solpos.index.dayofyear, label=None)
115-
fig.colorbar(points)
123+
c=solpos.index.dayofyear, label=None,
124+
cmap='twilight_shifted_r')
125+
# add and format colorbar
126+
cbar = fig.colorbar(points)
127+
times_ticks = pd.date_range('2019-01-01', '2020-01-01', freq='MS', tz=tz)
128+
cbar.set_ticks(ticks=times_ticks.dayofyear, labels=[], minor=False)
129+
cbar.set_ticks(ticks=times_ticks.dayofyear+15,
130+
labels=times_ticks.strftime('%b'),
131+
minor=True)
132+
cbar.ax.tick_params(which='minor', width=0)
116133

117134
for hour in np.unique(solpos.index.hour):
118135
# choose label position by the largest elevation for each hour
119136
subset = solpos.loc[solpos.index.hour == hour, :]
120137
height = subset.apparent_elevation
121138
pos = solpos.loc[height.idxmax(), :]
122-
ax.text(pos['azimuth'], pos['apparent_elevation'], str(hour))
139+
azimuth_offset = -10 if pos['azimuth'] < 180 else 10
140+
ax.text(pos['azimuth']+azimuth_offset, pos['apparent_elevation'],
141+
str(hour).zfill(2), ha='center', va='bottom')
123142

124143
for date in pd.to_datetime(['2019-03-21', '2019-06-21', '2019-12-21']):
125144
times = pd.date_range(date, date+pd.Timedelta('24h'), freq='5min', tz=tz)
126145
solpos = solarposition.get_solarposition(times, lat, lon)
127146
solpos = solpos.loc[solpos['apparent_elevation'] > 0, :]
128-
label = date.strftime('%Y-%m-%d')
147+
label = date.strftime('%d %b')
129148
ax.plot(solpos.azimuth, solpos.apparent_elevation, label=label)
130149

131-
ax.figure.legend(loc='upper left')
150+
ax.figure.legend(loc='upper center', bbox_to_anchor=[0.45, 1], ncols=3)
132151
ax.set_xlabel('Solar Azimuth (degrees)')
133152
ax.set_ylabel('Solar Elevation (degrees)')
153+
ax.set_xticks([0, 90, 180, 270, 360])
154+
ax.set_ylim(0, None)
134155

135156
plt.show()

docs/sphinx/source/whatsnew/v0.11.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,4 @@ Contributors
9090
* Ioannis Sifnaios (:ghuser:`IoannisSifnaios`)
9191
* Mark Campanelli (:ghuser:`markcampanelli`)
9292
* Rajiv Daxini (:ghuser:`RDaxini`)
93+
* :ghuser:`PhilBrk8`

0 commit comments

Comments
 (0)