【问题标题】:Color and Line writing using MatPlotLib使用 MatPlotLib 书写颜色和线条
【发布时间】:2012-04-20 22:07:03
【问题描述】:

我正在尝试使用 Matplotlib 绘制曲线族。我直接使用scatter() 绘制数据,然后使用plot() 绘制拟合线(scipy 的最小二乘)。不知道事先会有多少组数据,或者限制等。

我需要能够循环这些线和点的颜色,以便一组数据中的所有内容都匹配。 Plot 使用一些内部默认值旋转颜色,并且 scatter 作为所有一种颜色出现。数据集可以靠得很近,所以只是假设从哪些点接近哪条拟合线是不够好的,并且因为我不知道手动制作颜色会有多少条曲线选择是不可扩展的。

此外,因为这些是曲线族(想想晶体管图),我需要能够用曲线显示相关标签。我想做的是将信息写在适合线本身上。

有谁知道这两种方法的好方法吗?

【问题讨论】:

    标签: python matplotlib graphing


    【解决方案1】:

    这试图回答您的所有问题。 下面的代码最多循环 7 种颜色。如果您需要更多,您应该创建一个更复杂的生成器,如另一个答案所示。

    import numpy as np
    from matplotlib import pyplot as plt
    
    def get_color():
        for item in ['r', 'g', 'b', 'c', 'm', 'y', 'k']:
            yield item
    
    x = 0.3 * np.array(range(40))
    
    color = get_color()
    
    for group in range(5):
        # generates a collection of points
        y = np.exp2(x + 0.5 * group)
        # fit to a polynomial
        z = np.polyfit(x, y, 6)
        p = np.poly1d(z)
    
        acolor = next(color)
        
        plt.scatter(x, y, color=acolor, marker='o')
        plt.plot(x, p(x), acolor + '-', label=str(group))
    
    plt.legend()
    plt.xlim((0, 15))
    plt.show() 
    

    上面代码中的生成器对于示例来说有点矫枉过正,但它为更复杂的计算提供了结构。如果你只需要几种颜色,你可以使用一个简单的迭代器

    >>> color = iter(list_of_colors)
    >>> acolor = next(color)
    

    如果需要无限循环,可以使用itertools.cycle

    >>> from itertools import cycle
    >>> color = cycle(['r', 'g', 'b', 'c', 'm', 'y', 'k'])
    >>> next(color)
    'r'
    >>> 
    

    编辑:您有多种选择来获得 n 种不同的颜色。正如我之前指出的,您可以使用其他答案中指示的方法使用生成器。例如,用不同的生成器替换 get_color

    import colorsys
    import numpy as np
    from matplotlib import pyplot as plt
    
    def get_color(color):
        for hue in range(color):
            hue = 1. * hue / color
            col = [int(x) for x in colorsys.hsv_to_rgb(hue, 1.0, 230)]
            yield "#{0:02x}{1:02x}{2:02x}".format(*col)
        
    x = 0.3 * np.array(range(40))
    
    color = get_color(15)
    
    for group in range(15):
        # generates a collection of points
        y = np.exp2(x + 0.5 * group)
        # fit to a polynomial
        z = np.polyfit(x, y, 6)
        p = np.poly1d(z)
    
        acolor = next(color)
    
        plt.scatter(x, y, color=acolor, marker='o')
        plt.plot(x, p(x), color=acolor, linestyle='dashed', label=str(group))
    
    plt.legend()
    plt.xlim((0, 15))
    plt.show() 
    

    你会得到 15 种不同的颜色。

    然而,相似的颜色是连续的,无法提供良好的分辨率/对比度。您可以通过跳过色调值来增加对比度:

    for hue in range(0, color*3, 3):
    

    绘制多条线时的另一个问题是图例......

    【讨论】:

    • 谢谢。这解决了眼前的问题,并迫使我了解我不知道存在的 yield 和 next 语句。但是,如果您需要任意数量的颜色,您会怎么做?例如,假设我有 100 个数据集,每个数据集都需要完全不同(即循环不好)。您将如何生成颜色迭代器?可以不显式编码数据集的数量,这样代码是可扩展的吗?
    【解决方案2】:

    我有一个类似的情况,我想为多行提供相同的颜色,同时仍然支持任意数量的行,而不需要手动定义它们。这是我想出的生成颜色的函数:

    import colorsys
    
    def get_colors(i, total):
        hue = i*(1.0/total)
        dark = [int(x) for x in colorsys.hsv_to_rgb(hue, 1.0, 100)]
        light = [int(x) for x in colorsys.hsv_to_rgb(hue, 1.0, 230)]
        return "#{0:02x}{1:02x}{2:02x}".format(*dark), "#{0:02x}{1:02x}{2:02x}".format(*light)
    

    如您所见,它会在深色和浅色版本中生成具有最大距离的total 颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      相关资源
      最近更新 更多