Add colormap to polar plot

Question:

I’m trying to make a semi-circular polar plot like the one that comes with the code. I would like to add a ColorBar, to show which colours are associated to which values of ‘v’, but I am not able to do it, since I don’t really understand how it works.

import numpy as np
import matplotlib.pyplot as plt
from math import sin, radians

# Theta and R
azimuths = np.radians(np.linspace(180, 360, 20))
zeniths = np.arange(0, 1000, 10)

# Some constant
v0 = 1
A=2.4e-24
alpha=radians(10)
n=3

r, theta = np.meshgrid(zeniths, azimuths)

# Function to plot
v = v0 - 2*A/(n+1) * (0.5*917*9.8 * sin(alpha))**n * r**(n+1)

fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
plt.figure(1)

ax.set_thetamin(180)
ax.set_thetamax(360)
ax.contourf(theta, r, v, cmap='coolwarm')
plt.show()

I’ve tried to add

plt.imshow(v)
plt.colorbar()

and it adds a color bar but it changes the plot. I guess the solution is something simple but I can’t find it, if someone could please help me understand how it works, thank you 🙂

Asked By: Matte

||

Answers:

Provide the output of contourf as the mappable to colorbar:

cont = ax.contourf(theta, r, v, cmap='coolwarm')
fig.colorbar(cont, shrink=.6, pad=.1)

enter image description here

Answered By: Stef
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.