用插值颜色填充阿基米德脊椎之间的区域-Matplotlib [英] Filling the area between Archimedes spyrals with interpolated colours - Matplotlib

查看:62
本文介绍了用插值颜色填充阿基米德脊椎之间的区域-Matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制如图所示的各种螺旋线(一个螺旋线在其他螺旋线内).假设我有三个螺旋线(S1,S2和S3),我想填充连续螺旋线之间的区域,即S1和S2,S2和S3之间的区域,最后是S3和S1之间的区域.

I want to plot various spirals like the ones shown in the Figure (one spiral inner the others). Let's say I have three spirals (S1, S2 and S3) and I want to fill the area between consecutive spirals, i.e. the area between S1 and S2, S2 and S3 and finally between S3 and S1.

我尝试了几种方法来解决以下两个问题,但都没有成功:

I have tried several ways to solve the following two problems but without any success:

1-一对螺旋之间的区域应涂上内插颜色.例如,第一个螺旋(S1)是黑色的,第二个(S2)是灰色的,那么S1和S2之间的区域一定是从黑色到灰色的渐变.

1- The area between a pair of spirals should be painted with interpolated colours. For example, the first spiral (S1) is black and the second one (S2) is gray, then the area between S1 and S2 must be a gradient that goes from black to gray.

2- S3 和 S1 之间的区域没有按照我的意愿填充.在这里,填充的区域在S1和S3之间,而不是在S3和S1之间.

2- The area between S3 and S1 has not being filled as I want. Here, the area filled is between S1 and S3, not between S3 and S1.

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt

def create_archimides_spiral(a=0, b=1, num_spirals=10):

    """
    Functions that creates an archimides spiral
    """

    th = np.linspace(0, num_spirals*np.pi, num_spirals*100) # The higher the number of splits, the greater the quality of each segment

    r = a + b*th

    x = r * np.cos(th)
    y = r * np.sin(th)

    return x, y

# create the spirals
x, y = create_archimides_spiral(a=0,b=1)
x1, y1 = create_archimides_spiral(a=2, b=1)
x2, y2 = create_archimides_spiral(a=4, b=1)

fig, ax = plt.subplots(1)
ax.plot(x, y, color='black', linewidth=3)
ax.plot(x1, y1, color='gray', linewidth=3)
ax.plot(x2, y2, color='silver', linewidth=3)

plt.fill(
    np.append(x, x1[::-1]),
    np.append(y, y1[::-1]), color= "lightgray"
)

plt.fill(
   np.append(x1, x2[::-1]),
   np.append(y1, y2[::-1]), color= "lightgray"
)

plt.fill_between(x, y, y1, interpolate=True)
plt.fill_between(x1, y, y1, interpolate=True)

ax.set_aspect(1)
plt.show()

这是我得到的数字,但这不是我想要的.

This is the figure that I obtained, but this is not that I want.

我将不胜感激.

最佳推荐

奥斯卡

推荐答案

您可以绘制许多彼此接近的螺旋,范围从0到2 pi.(2 pi 是连续环之间的距离.)

You could draw many spirals close to each other, in the range from 0 to 2 pi. (2 pi is the distance between the successive rings.)

注意颜色值为'0.20'(一串数字)对应的灰度值为0.20,其中0是黑色的,1 是白色的.

Note that a color value as '0.20' (a string of a number) corresponds to a grey value of 0.20, where 0 is black and 1 is white.

还请注意, plt.fill_between()假定x值是有序的.

Also note that plt.fill_between() supposes the x-values are ordered, which isn't the case.

要获得完全填充的螺旋,您可以向方程添加起始角度,如中央子图所示.右侧子图使用两种颜色之间的插值.

To obtain a fully filled spiral, you could add a start angle to your equation as shown in the central subplot. The right subplot uses an interpolation between two colors.

import numpy as np
import matplotlib.pyplot as plt


def create_archimides_spiral(a=0, b=1, start_angle=0, num_spirals=10):
    th = np.linspace(0, num_spirals * np.pi, num_spirals * 100)
    r = a + b * th
    x = r * np.cos(th + start_angle)
    y = r * np.sin(th + start_angle)
    return x, y


fig, axes = plt.subplots(ncols=3)
for ax, largest_a in zip(axes, [2 * np.pi, 2 * np.pi, 1.5 * np.pi]):
    a_values = np.linspace(0, largest_a, 100)
    grey_values = np.linspace(0, 0.8 if ax == axes[0] else 1, 100)
    if ax == axes[1]:
        cmap = plt.cm.inferno
    elif ax == axes[2]:
        cmap = plt.cm.colors.LinearSegmentedColormap.from_list('', ['crimson', 'skyblue'])
    for a, grey in zip(a_values, grey_values):
        if ax == axes[1]:
            x, y = create_archimides_spiral(a=0, b=1, start_angle=a)
        else:
            x, y = create_archimides_spiral(a=a, b=1, start_angle=0)
        ax.plot(x, y, color=f'{grey:.3f}' if ax == axes[0] else cmap(grey))
    ax.set_aspect('equal')
plt.show()

这篇关于用插值颜色填充阿基米德脊椎之间的区域-Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆