简体   繁体   English

使用 matplotlib fill_between 填充两条极曲线的公共内部

[英]Fill the common interior of two polar curves with matplotlib fill_between

I'm trying to fill the common interior of circle and cardioid in polar coordinates (r1= sqrt(3)* sin(φ) and r2= 1 - cos(φ)).我试图在极坐标(r1= sqrt(3)* sin(φ) 和 r2= 1 - cos(φ))中填充圆和心形的共同内部。 I've found a simple solution in this question — find the minimum of two functions and fill an area between 0 and minimum.我在这个问题中找到了一个简单的解决方案 - 找到两个函数中的最小值并填充 0 和最小值之间的区域。 Code:代码:

import matplotlib.pyplot as plt
import numpy as np
import math

phi =  np.arange(0, (2 * np.pi), 0.01)
r1 = math.sqrt(3) * np.sin(phi)
r2 = 1 - np.cos(phi)
r3 = np.minimum(r1, r2)

plt.polar(phi, r1)
plt.polar(phi, r2)
plt.fill_between(phi, 0, r3, color='#D0D0FF')
plt.show()

But the end result is strange.但最终的结果很奇怪。 The bottom part, where φ goes from 2π to 0, is 'inverted'.底部,其中 φ 从 2π 变为 0,是“倒置的”。

Resulting plot结果图

Perhaps I don't understand the 'where' argument of 'fill_between'.也许我不理解“fill_between”的“where”参数。 Any ideas?有任何想法吗?

Your polar plot's radial axis has a range that goes from approximately -1.7 to 2.极坐标图的径向轴范围约为 -1.7 到 2。

So with your call plt.fill_between(phi, 0, r3, color='#D0D0FF') you're missing the part between -1.7 and 0 when the cradiod dips below 0.因此,通过调用plt.fill_between(phi, 0, r3, color='#D0D0FF')当 crradiod 跌至 0 以下时,您会丢失 -1.7 和 0 之间的部分。

This modified code will work : I changed the 0 in fill_between to min(r1) and the r-axis limits to min(r1) and max(r2) to fit the plot.此修改后的代码将起作用:我将fill_between中的 0 更改为min(r1)并将 r 轴限制为min(r1)max(r2)以适应绘图。

import matplotlib.pyplot as plt
import numpy as np
import math

phi =  np.arange(0, (2 * np.pi), 0.01)
r1 = math.sqrt(3) * np.sin(phi)
r2 = 1 - np.cos(phi)
r3 = np.minimum(r1, abs(r2))

plt.polar(phi, r1)
plt.polar(phi, r2)
plt.ylim(min(r1),max(r2))
plt.fill_between(phi, min(r1), r3, color='#D0D0FF')
plt.show()

在此处输入图像描述

Hope this helps !希望这可以帮助 !

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

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