A few weeks ago, I read an interesting article about watermarking your ggplot charts in R. R is certainly a fantastic tool, but as my go-to language for visualizations these days is Python, I had to ask myself, “self, how would you watermark your matplotlib charts?” Well, one answer is the text method of the Figure object.
Consider this polar chart I wrote some time ago:
colleges = ['College A', 'College B', 'College C', 'College D', 'College E']
scores = [76, 54, 58, 63, 65]
theta = np.arange(len(colleges))/float(len(colleges)) * 2 * np.pi
fig = plt.figure(figsize=(8, 8))
ax = plt.subplot(111, projection='polar')
ax.plot(theta, scores, color='green', marker='o')
ax.plot([theta[0], theta[-1]], [scores[0], scores[-1]], 'g-') # hack to complete the circle
ax.set_rmax(max(scores) + 5)
ax.set_rticks(np.arange(0, max(scores) + 5, step=10))
ax.set_xticks(theta)
labels = ax.set_xticklabels(colleges)
# hack to get the labels to show nicely
[l.set_ha('right') for l in labels if l.get_text() in ['College C', 'College D']]
[l.set_ha('left') for l in labels if l.get_text() in ['College A']]
for i, txt in enumerate(scores):
ax.annotate(txt, (theta[i], scores[i]))
ax.grid(True)
ax.set_title('College Scorecard')
plt.tight_layout()
# watermarking my chart
fig.text(0.95, 0.06, 'DadOverflow.com',
fontsize=12, color='gray',
ha='right', va='bottom', alpha=0.5)
plt.show()
I’ve highlighted the part of the code that watermarks the chart. This code produces the following chart:
Hey, how about that cool watermark in the lower right-hand corner? Snazzy, right? One challenge I’ve found is that as you change the size of your chart, you’ll have to play around a little with the x and y coordinates of your watermark. Nevertheless, this seems to me like a great way to brand your charts.
Recent Comments