2

Thanks to this very helpful post, I finally figured out how to make a polar filled-contour plot. However, when I moved to the next step and tried to add a scatter point to the same plot, I ran in some problems. Here is the original script:

import numpy as np
import matplotlib.pyplot as plt

#-- Generate Data -----------------------------------------
# Using linspace so that the endpoint of 360 is included...
azimuths = np.radians(np.linspace(0, 360, 20))
zeniths = np.arange(0, 70, 10)

r, theta = np.meshgrid(zeniths, azimuths)
values = np.random.random((azimuths.size, zeniths.size))

#-- Plot... ------------------------------------------------
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(theta, r, values)

plt.show()

Which produced this image: Polar filled contour without scatter

If I add also a scatter plot:

#-- Plot... ------------------------------------------------
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(theta, r, values)
ax.scatter(np.radians(140), 40, s=20, c='White')

I get instead this image:

Polar filled contour with scatter

Why do I get the thick white border between the filled contours and the axes? How do I get rid of it?

Thank you very much!

Community
  • 1
  • 1
Cronopio
  • 141
  • 1
  • 12

1 Answers1

2

Ops, sorry the answer occurred to me two minutes after a asked my question. I just realized that adding a scatter plot somehow changes the axes limits. Forcing the axes to the desired interval fixes the problem.

fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(theta, r, values)
ax.scatter([np.radians(140)], [40], s=20, c='White')
ax.set_rmax(60)
ax.set_rmin(0)

enter image description here

I thought I could leave the question on anyways, it still can be helpful to other users.

Cronopio
  • 141
  • 1
  • 12
  • 2
    +1 What happens is that `ax.scatter` uses "loose" autoscaling (i.e. it chooses even-ish numbers as axes limits) while `ax.contourf` uses "tight" autoscaling (i.e. it strictly uses the data limits). Instead of manually setting the limits, you could just call `ax.autoscale(False)` after calling `contourf` and before calling `scatter`. – Joe Kington Mar 12 '14 at 18:38