Drimades Boy
Drimades Boy

Reputation: 417

Label ranges and colors in Python plot

In the following I'm trying to plot points with color and label that changes for every j. What I'm getting is different color and one label in the legend for every couple (i, j):

import itertools
import numpy as np
import matplotlib.pyplot as plt

color_list = ["blue", "brown", "red", "black"]
g = itertools.cycle(color_list)

primes = (139,149,151,157,163,167)
for j in range(50, 300, 10):
    col = next(g)
    for k in (primes):
        for l in range(10000, 20000, 500):
            start = time.time()
            generator(j, k, l)
            end = time.time()
            plt.plot(l, end - start, linewidth = 2, marker='o', color=col)

Edit: Updated the code with my original problem as I couldn't figure out if the solution from Ernest + Arya can work in this case.

Upvotes: 0

Views: 2373

Answers (3)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339765

Isn't the second loop over i pretty useless?

import numpy as np
import matplotlib.pyplot as plt

for j in range (0, 2):
    x = np.arange(0,9,2)
    y = np.ones_like(x)*j + 0.5
    plt.plot(x,y, marker='o', label='$n = {j}$'.format(j=j))

plt.legend()    
plt.show()

enter image description here

Solution for updated question:

import itertools
import numpy as np
import matplotlib.pyplot as plt

color_list = ["blue", "brown", "red", "black"]
g = itertools.cycle(color_list)

for j in range (0, 2):
    col = next(g)
    x = np.arange(0,9,2)
    y = np.ones_like(x)*j + 0.5
    plt.plot(x,y, marker='o', color=col, label='$n = {j}$'.format(j=j))
plt.legend()  
plt.show()

enter image description here

Solution for second update; the idea is always the same: use as many calls to plt.plot() as you want to have different colors/legend entries.

import numpy as np
import matplotlib.pyplot as plt
import time

def generator(j,k,l):
    #some operation that takes a little time
    a = np.random.rand(k % 7, l-1000, j%13 )
    return a

color_list = ["blue", "brown", "red", "black"]

primes = (139,149,151,157,163,167)
for j in range(50, 300, 25):
    x = []; y = []
    for k in (primes):
        for l in range(10000, 20000, 1000):
            start = time.time()
            generator(j, k, l)
            end = time.time()
            x.append(l); y.append(end - start)
    plt.plot(x,y, ls="", marker='o', label='$n = {j}$'.format(j=j))

plt.legend()
plt.show()

enter image description here

Upvotes: 1

litepresence
litepresence

Reputation: 3277

you can use sin or cos functions to control color choice and add a variable that changes only when j changes to control whether you plot a new label

import numpy as np
import matplotlib.pyplot as plt
import math

last_j = -1
z = 5
for j in range (0, z):
    color = ((math.sin(j)+1)/2,(math.cos(j+1)+1)/2,(math.cos(j)+1)/2)
    for i in range (0, 10, 2):
        if j == last_j:
            plt.plot(i + 0.5, j + 0.5, c=color, linewidth = 2, marker='o')
        else:
            plt.plot(i + 0.5, j + 0.5, c=color, linewidth = 2, marker='o', 
                label='$n = {j}$'.format(j=j))
        last_j = j

ax = plt.gca()
legend = ax.legend(loc = 'center right', shadow=True)

plt.show()

enter image description here

Upvotes: 1

Arya McCarthy
Arya McCarthy

Reputation: 8814

You can use the matplotlib cycler. Set up the list of colors you want to iterate over, then inside the j-loop, get a color with my_cycle.next(). Then pass that color into your call to plt.plot.

Upvotes: 1

Related Questions