【问题标题】:How to identify certain coordinates (x,y) for its color after matplotlib.pyplot fill function?如何在 matplotlib.pyplot 填充函数后识别其颜色的某些坐标(x,y)?
【发布时间】:2020-11-15 06:08:25
【问题描述】:

有什么方法可以将某个坐标(x,y)输入到pyplot并输出其填充颜色作为回报?

例如。 (0,0) -> 红色 , (0.75,0) -> 蓝色 , (1,1) ->白色

import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(15,15))
x,y,x2,y2=[],[],[],[]
for i in np.linspace(0,2,300):
    x.append(np.cos(np.pi*i))
    y.append(np.sin(np.pi*i))  
    x2.append(0.5*np.cos(np.pi*i))
    y2.append(0.5*np.sin(np.pi*i))

plt.fill(x,y,'b')
plt.fill(x2,y2,'r')

Color Image

【问题讨论】:

  • 你为什么要那个?你的实际情节是plt.imshow 还是plt.plot
  • 嗨,我需要的更像是我将其保存为图片(不是字面意义上的保存文件),并查找其颜色(不是通过像素位置,而是通过其原始坐标)。
  • 请澄清:1.你如何“保存为图片”? 2.什么是“不按字面保存文件”? 3、什么是“原坐标”? 4. 你想要的预期输入和输出是什么?
  • 1&2。就像您的回答一样,所有操作都由画布完成,而不是将文件保存到某个路径(例如:d:/123.png)
  • 3&4。我想输入它的原始 x,y 而不是像素位置 (0,0) -> Red , (0.75,0) -> blue , (1,1) ->white, 输出将是什么颜色跨度>

标签: python matplotlib fill


【解决方案1】:

这是一种可能的方法。让我们从修改后的代码(基于您的)开始:

# Modified code
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import Point, Polygon  # additional code

plt.figure(figsize=(15,15))
x,y,x2,y2=[],[],[],[]
for i in np.linspace(0,2,300):
    x.append(np.cos(np.pi*i))
    y.append(np.sin(np.pi*i))  
    x2.append(0.5*np.cos(np.pi*i))
    y2.append(0.5*np.sin(np.pi*i))

# grab the fill objects for use later
# .fill return: matplotlib.patches.Polygon object
blue_pgn = plt.fill(x,y,'b')  #Plot filled polygons
red_pgn = plt.fill(x2,y2,'r')

plt.show()

它产生与您的代码相同的情节,但也公开了 2 个有用的对象,blue_pgnred_pgn

这是第二部分:

# Create geometries from objects in the plot
blue_geom = Polygon(blue_pgn[0].get_xy())
red_geom =  Polygon(red_pgn[0].get_xy())

# create a function
def from_xy_to_color(x,y):
    point_xy = Point(x,y)
    if red_geom.contains(point_xy):
        print("xy:",x,y,", color:","Red")
    elif blue_geom.contains(point_xy):
        print("xy:",x,y,", color:","Blue")
    else:
        print("xy:",x,y,", color:","White")

# test all the points
xys = [[0,0], [.75,0], [1,1]]
for xy in xys:
    #print(xy)
    from_xy_to_color(*xy)

这部分的输出:

xy: 0 0 , color: Red
xy: 0.75 0 , color: Blue
xy: 1 1 , color: White

【讨论】:

  • 嗨,我需要的更像是我将它保存为图片(不是字面上保存文件),并查找它的颜色(不是通过像素位置,而是通过其原始坐标)。您的建议是此处示例的好方法。但是我发现如果将来颜色区域变得越来越复杂,定义每个多边形将非常困难。你知道其他方法吗?
  • @allenwei 多边形对象有许多有用的函数/方法来操纵自己(尝试运行print(dir(blue_geom)))。您可以检查几何是否有效。如果多边形过于杂乱,您可以简化它的几何图形以消除它的一些顶点并使用结果来代替。您可以将几何图形转换为wkt 格式并将它们保存在文件中以供将来使用。如果您想了解有关这些内容的更多详细信息,请作为新问题提出,因为这在本问题的范围内是题外话。
  • @allenwei 仅供参考。对于新用户,在您获得一些声望点之前,您无法投票赞成我的答案。但是您可以将其标记为“已接受”,并获得“+2”声望。 stackoverflow.com/help/someone-answers
【解决方案2】:

from matplotlib.backends.backend_agg import FigureCanvasAgg
import numpy as np
import matplotlib.pyplot as plt


def circle_coords():
    t = np.linspace(0, 2, 300) * np.pi
    x = np.cos(t)
    y = np.sin(t)
    return x, y


def plot(fig):
    ax = fig.gca()
    x, y = circle_coords()
    ax.fill(x, y, 'b')
    ax.fill(x / 2, y / 2, 'r')


def capture():
    plt.Figure((4, 4), dpi=20)
    fig = plt.gcf()
    canvas = FigureCanvasAgg(fig)
    plot(fig)
    canvas.draw()
    r = canvas_to_numpy(canvas)
    plt.close()
    return r


def canvas_to_numpy(canvas):
    s, (width, height) = canvas.print_to_buffer()
    x = np.frombuffer(s, np.uint8)
    return x.reshape((height, width, 4))


def random_points(shape, n_points=4):
    height, width = shape
    x = np.random.uniform(0, width, size=n_points)
    y = np.random.uniform(0, height, size=n_points)
    return np.vstack([x, y]).T


def main():
    arr = capture()

    p = [[360, 274],
         [379, 48],
         [117, 216]]

    fig = plt.gcf()
    ax = fig.gca()

    np.set_printoptions(precision=2)
    print(p)
    for x, y in p:
        r, g, b, a = arr[y, x] / 255
        c = f"{r, g, b}"
        print(arr[y, x])
        ax.text(x, y, c)
        ax.scatter(x, y, c="yellow")
    plt.imshow(arr)
    plt.show()
    plt.close()


main()

见:Matplotlib figure to image as a numpy array

【讨论】:

  • 这个答案差不多了,而输入 x,y 不是原始坐标,例如:中心点 (x,y) = (0,0) 而不是 (320,250)跨度>
猜你喜欢
  • 2015-09-28
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多