【问题标题】:Shading the area of a triangle对三角形区域进行着色
【发布时间】:2021-08-02 15:27:19
【问题描述】:

这是我的代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x = np.random.randint(1,100,100)
y = np.random.randint(-500,200,100)
plt.scatter(x, y)
ax = plt.gca()
ax.invert_yaxis()


x1, y1 = [0, 100], [-200, 0]
x2, y2 = [0, 0], [0, -200]
x3, y3 = [0, 100], [0, 0]

plt.plot(x1,y1,x2,y2,x3,y3, marker = 'o')
plt.show()

plt.show()

有两个问题

  1. 我希望三角形的手臂颜色相同,我该怎么做?
  2. 我想对三角形下方的区域进行阴影处理,使其半透明,即我可以看到三角形内部散点图的点。这可行吗?

【问题讨论】:

    标签: python matplotlib plot scatter-plot


    【解决方案1】:

    您可以通过列出 3 个 xy 坐标来表示一个三角形。重复第一个点会创建一个封闭的多边形。转换为 numpy 可以更轻松地仅选择 x 坐标和仅选择 y 坐标,如 triangle[:,0] 中的 x 坐标。

    fill 创建一个填充多边形,其中alpha 带来半透明。

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.random.randint(1, 100, 100)
    y = np.random.randint(-500, 200, 100)
    ax = plt.gca()
    ax.scatter(x, y)
    ax.invert_yaxis()
    
    points = [[0, -200], [100, 0], [0, 0]]
    triangle = np.array(points + points[:1])
    
    ax.plot(triangle[:, 0], triangle[:, 1], marker='o')
    ax.fill(triangle[:, 0], triangle[:, 1], color='yellow', alpha=0.3)
    
    for xi, yi in points:
        ax.text(xi, yi, f' x:{xi:.0f}\n y:{yi:.0f}', color='red', fontsize=12, weight='bold',
                va='top' if yi > -100 else 'bottom')
    ax.set_xlim(xmax=120) # more space for text
    
    plt.show()
    

    PS:此答案使用fill,因为它适用于一般三角形(以及更复杂的多边形)。 fill_between(x1, y1) 填充线段 x1,y1y=0 之间的区域。如问题示例中的y3 无处不在0fill_between(x1, y1) 将为示例三角形着色。如果y3 具有不同的值,但x3x1 仍然相等,则fill_between(x1, y1, y3) 将起作用。对于更任意的三角形,fill_between 会更麻烦。

    【讨论】:

    • btw 有一个问题,如何在图表上显示三角形坐标?即 (0, -200), (100, 0), (0, 0)
    • 我添加了一些代码来添加带有坐标的文本。
    【解决方案2】:

    您只需要在plot() 中指定color 属性即可。至于阴影,你需要fill_between()函数:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    x = np.random.randint(1,100,100)
    y = np.random.randint(-500,200,100)
    
    ax = plt.gca()
    ax.scatter(x, y)
    ax.invert_yaxis()
    
    x1, y1 = [0, 100], [-200, 0]
    x2, y2 = [0, 0], [0, -200]
    x3, y3 = [0, 100], [0, 0]
    
    ax.plot(x1,y1,x2,y2,x3,y3, marker = 'o', color='red')
    ax.fill_between(x1, y1, facecolor='red', alpha=0.5)
    plt.show()
    

    输出:

    【讨论】:

      【解决方案3】:

      更一般地说,您可以使用 matplotlib 补丁和集合 (example) 填充任何多边形:

      import numpy as np
      import pandas as pd
      import matplotlib.pyplot as plt
      from matplotlib.patches import Polygon
      from matplotlib.collections import PatchCollection    
      
      x = np.random.randint(1,100,100)
      y = np.random.randint(-500,200,100)
      plt.scatter(x, y)
      ax = plt.gca()
      ax.invert_yaxis()
      
      
      vertices = np.array([[0,0],[0,-200.0],[100.0,0]])
      
      patches = []
      triangle = Polygon(vertices, True)
      patches.append(triangle)
      
      p = PatchCollection(patches, alpha=0.4)
      ax=plt.gca()
      ax.add_collection(p)
      
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-05
        • 2020-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多