0

I want to superimpose Circles on top of a 2D plot in Matplotlib as position markers. Currently they look very much like what they are, colored Circles:

import matplotlib.pyplot as plt

plt.axes()

circle = plt.Circle((0, 0), radius=0.3,fc='b')
circle1 = plt.Circle((1, 1), radius=0.3, fc='y')
circle2 = plt.Circle((1, 0), radius=0.3, fc='r')
plt.gca().add_patch(circle)
plt.gca().add_patch(circle1)
plt.gca().add_patch(circle2)

plt.axis('scaled')
plt.show()

Is there a way to give theses circles the appearance of a 3D object without using mayavi?

These are examples of my goal:

enter image description here

EDIT

With the information in the link supplied by user3419537 and the idea presented here Custom color maps i created the following idea, that lets me somehow plot circles filled with a gradient:

import numpy as np

import matplotlib.colors as mcolors


def make_colormap(seq):
    """Return a LinearSegmentedColormap
    seq: a sequence of floats and RGB-tuples. The floats should be increasing
    and in the interval (0,1).
    """
    seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3]
    cdict = {'red': [], 'green': [], 'blue': []}
    for i, item in enumerate(seq):
        if isinstance(item, float):
            r1, g1, b1 = seq[i - 1]
            r2, g2, b2 = seq[i + 1]
            cdict['red'].append([item, r1, r2])
            cdict['green'].append([item, g1, g2])
            cdict['blue'].append([item, b1, b2])
    return mcolors.LinearSegmentedColormap('CustomMap', cdict)



def gauplot(centers, radiuses, xr=None, yr=None, P_color='black'):
        c = mcolors.ColorConverter().to_rgb
        # Maybe it is possible to change the values to get a better gradient?
        current_cmap = make_colormap([c(P_color),0.05,c(P_color),0.1,c(P_color), c('white')])               
        nx, ny = 1000.,1000.
        xgrid, ygrid = np.mgrid[xr[0]:xr[1]:(xr[1]-xr[0])/nx,yr[0]:yr[1]:(yr[1]-yr[0])/ny]
        im = xgrid*0 + np.nan
        xs = np.array([np.nan])
        ys = np.array([np.nan])
        fis = np.concatenate((np.linspace(-np.pi,np.pi,100), [np.nan]) )
        #cmap = plt.cm.gray
        cmap = current_cmap
        cmap.set_bad('white')
        thresh = 2.8
        for curcen,currad in zip(centers,radiuses):
                curim=(((xgrid-curcen[0])**2+(ygrid-curcen[1])**2)**.5)/currad*thresh
                im[curim<thresh]=np.exp(-.5*curim**2)[curim<thresh]
                xs = np.append(xs, curcen[0] + currad * np.cos(fis))
                ys = np.append(ys, curcen[1] + currad * np.sin(fis))
        plt.imshow(im.T, cmap=cmap, extent=xr+yr)
        plt.plot(xs, ys, 'r-')

gauplot([(0,0), (2,3), (5,1), (6, 7), (6.1, 6.1)], [.3,.4, .5, 1, .4], [-1,10], [-1,10],P_color="#75507b")

plt.show()        

Unfortunately only the red circles appear at the right position:

OUTPUT

I would appreciate a tip what might be the cause of this.

At the moment i am plotting my circles in the script in question like this:

circle = Circle(x,y,*kwargs)
plt.gca().add_patch(circle)

would it be possible to adapt the above solution in a way that it can plot circles with different colors at the position (x,y) and superimpose them to an existing plot as well?

NorrinRadd
  • 545
  • 6
  • 21

1 Answers1

0

The imshow plot is mirrored against the desired plot. This is because the imshow plot has its origin in the upper left corner, but the plot to show the circles has the origin in the lower left corner.

The solution would be to set the origin of the imshow in the lower left corner

plt.imshow(im.T, cmap=cmap, extent=xr+yr, origin="lower")
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712