繁体   English   中英

如何在python的极坐标图中显示笛卡尔系统?

[英]How to show Cartesian system in polar plot in python?

在这里,我试图在笛卡尔网格上添加极坐标图,但我得到的是2个独立的图形(一个极地另一个笛卡尔坐标),我希望这个极坐标图形嵌入笛卡尔图中。 我也使用了之前可用的一些代码,因为我是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

我不习惯使用matplotlib但是我将代码减少到最低限度以便更好地理解它并使其看起来更少减少。 看看我得到了什么:

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()

这几乎是一样的结果......

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM