2016-05-23 80 views
3

這裏我試圖在笛卡爾網格的頂部添加極座標圖,但是我得到的卻是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 

回答

2

我不習慣matplotlib但我你的代碼減少到最低限度他更好地理解它,讓它看起來不那麼redudant。看看我得到:

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

它幾乎是同樣的結果...

+0

謝謝,但我不wan't失去4個象限和電網。 – thedarkgriffen

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

也許嘗試總結一個單一的答案... –

相關問題