Color and line recording using MatPlotLib

I am trying to display curve families using Matplotlib. I draw the data directly using scatter (), and then draw the fit line (the smallest squares from scipy) using plot (). I do not know how many datasets will be done in advance, or limits, etc.

I need to be able to quote the colors of these lines and dots so that everything that matches a single dataset. The patch rotates colors using some internal defaults, and the scatter comes out as all one color. Datasets can be close to each other, so just moving on to the assumption that it will be clear from which points close, which line of the line is not enough, and since I don’t know how many curves will manually make the color the selection does not scale.

Also, since these are curve families (calculated transistor plots), I need to show the corresponding curve labeling. What I would like to do is write information about the match string itself.

Does anyone know of a good path to any of these?

+3
source share
2 answers

This is trying to answer all your questions. The code below shows no more than 7 colors. If you need more, you should create a simpler generator, as shown in another answer.

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() 

enter image description here

The generator in the above code is a bit redundant for an example, but it provides a structure for more complex calculation. If you need only a few colors, you can use a simple iterator

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

and if you need an infinite loop, you can use 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 .

enter image description here

, , , /. , :

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

- ...

+13

, , , . , :

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 .

+2
source

All Articles