Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add colormap to polar plot

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 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading