3

Here I tried to add the polar plot on top of the Cartesian grid,but what I got instead was 2 separate figures(one polar another Cartesian),I want this polar figure to be embedded in the Cartesian plot. Also I have used some of the code previously available as I am new to matplotlib.

from pylab import *
import matplotlib.pyplot as plt
x = [0,10,-3,-10]
y = [0,10,1,-10]
color=['w','w','w','w']

fig = plt.figure()
ax1 = fig.add_subplot(111)

scatter(x,y, s=100 ,marker='.', c=color,edgecolor='w')

circle1=plt.Circle((0,0),5,color='r',fill=False)
circle_min=plt.Circle((0,0),4.5,color='g',fill=False)
circle_max=plt.Circle((0,0),5.445,color='b',fill=False)
fig = plt.gcf()


fig.gca().add_artist(circle1)
fig.gca().add_artist(circle_min)
fig.gca().add_artist(circle_max)

left,right = ax1.get_xlim()
low,high = ax1.get_ylim()
arrow( left, 0, right -left, 0, length_includes_head = True, head_width = 0.15 )
arrow( 0, low, 0, high-low, length_includes_head = True, head_width = 0.15 )


grid()

fig = plt.figure()
ax2 = fig.add_subplot(111)

scatter(x,y, s=100 ,marker='.', c=color,edgecolor='w')

circle2=plt.Circle((0,0),5,color='r',fill=False)
circle_min=plt.Circle((0,0),4.5,color='g',fill=False)
circle_max=plt.Circle((0,0),5.445,color='b',fill=False)
fig = plt.gcf()


fig.gca().add_artist(circle2)
fig.gca().add_artist(circle_min)
fig.gca().add_artist(circle_max)

left,right = ax2.get_xlim()
low,high = ax2.get_ylim()
arrow( left, 0, right -left, 0, length_includes_head = True, head_width = 0.15 )
arrow( 0, low, 0, high-low, length_includes_head = True, head_width = 0.15 )

import numpy as np
import matplotlib.pyplot as plt


theta = np.linspace(-np.pi, np.pi, 100)  
r1 = 1 - np.sin(3*theta)
r2 = 1 + np.cos(theta)


ax = plt.subplot(111, polar=True,      # add subplot in polar coordinates 
                 axisbg='Azure')       # background colour

ax.set_rmax(2.2)                       # r maximum value
ax.grid(True)                          # add the grid

ax.plot(theta, r1,
        color='Tomato',                # line colour
        ls='--',                       # line style
        lw=3,                          # line width
        label='a 3-fold curve')        # label

ax.plot(theta, r2, 
        color='purple',
        linewidth=3,
        ls = '-',
        label = 'a cardioid')


ax.legend(loc="lower right")           # legend location

titlefont = {
        'family' : 'serif',
        'color'  : 'black',
        'weight' : 'bold',
        'size'   : 16,
        }

ax.set_title("A plot in polar coordinates", # title
             va='bottom',                   # some space below the title
             fontdict = titlefont           # set the font properties
             )



grid()

show()



#I am getting a separate Cartesian image + a polar image while what I need   is both the things in a single image

2 Answers 2

2
import matplotlib.pyplot as plt
import numpy as np
#########################################
color=['w','w','w','w']
theta = np.linspace(-np.pi, np.pi, 100)  
fig = plt.figure()# initializing the figure
rect = [0.1, 0.1, 0.8, 0.8]# setting the axis limits in [left, bottom, width, height]
ax_carthesian  = fig.add_axes(rect)# the carthesian axis:
ax_polar = fig.add_axes(rect, polar=True, frameon=False)# the polar axis:
#########################################

ax_carthesian.add_artist(plt.Circle((0.5,0.5),5/15,color='r',fill=False))
ax_carthesian.add_artist(plt.Circle((0.5,0.5),4.5/15,color='g',fill=False))
ax_carthesian.add_artist(plt.Circle((0.5,0.5),5.445/15,color='b',fill=False))

ax_polar.plot(theta, 1 - np.sin(3*theta),   color='Tomato',ls='--',lw=1,        label='a 3-fold curve')
ax_polar.plot(theta, 1 + np.cos(theta),     color='purple',linewidth=1,ls = '-',label = 'a cardioid')


plt.show()
Sign up to request clarification or add additional context in comments.

Comments

2

I am not used to matplotlib but I reduced your code to his minimum to better understand it and make it look less redudant. look at what I get:

import pylab
import matplotlib.pyplot as plt
import numpy as np
#########################################
x = [0,10,-3,-10]
y = [0,10,1,-10]
color=['w','w','w','w']
theta = np.linspace(-np.pi, np.pi, 100)  
#########################################
pylab.scatter(x,y, s=100 ,marker='.', c=color,edgecolor='w')

plt.gcf().gca().add_artist(plt.Circle((0,0),5,color='r',fill=False))
plt.gcf().gca().add_artist(plt.Circle((0,0),4.5,color='g',fill=False))
plt.gcf().gca().add_artist(plt.Circle((0,0),5.445,color='b',fill=False))

plt.figure().add_subplot(111)
ax = plt.subplot(111, polar=True,axisbg='Azure')
ax.plot(theta, 1 - np.sin(3*theta),color='Tomato',ls='--',lw=3,label='a 3-fold curve')
ax.plot(theta, 1 + np.cos(theta),color='purple',linewidth=3,ls = '-',label = 'a cardioid')

pylab.show()

it is nearly the same result...

1 Comment

Thanks but I don't wan't to lose the 4 quadrants and the grid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.