Extracting row from matrix in Python and use that row as a color in Matplotlib

Question:

This should be easy, but I haven’t gotten the hang of Python syntax yet. I create an array like this:

colors = [ [(17.0/256.0), (15.0/256.0), (48.0/256.0)],  #Dark blue  (#110F30)
         [(239.0/256.0), (83.0/256.0), (25.0/256.0)]    #Orange     (#EF5319)
         ]

I the want to use the first or second row as a color input in the plot statement:

ax1.plot(time, temp - 273, color=colors[0,:], label=temp_axis_label)

However, this does not work (obviously). What is the correct syntax to extract the rows from the color matrix?

Asked By: Krøllebølle

||

Answers:

Simply : ax1.plot(time, temp - 273, color=colors[0], label=temp_axis_label)

The problem lies in the fact that colors is a list of list, not a numpy matrix
:

colors = np.array([ [(17.0/256.0), (15.0/256.0), (48.0/256.0)],  #Dark blue  (#110F30)
         [(239.0/256.0), (83.0/256.0), (25.0/256.0)]    #Orange     (#EF5319)
         ])
ax1.plot(time, temp - 273, color=colors[0,:], label=temp_axis_label)

works too.

Answered By: lucasg

drop the ,:

color = colors[0]
Answered By: Cameron Sparr

Simply do:

ax1.plot(time, temp - 273, color=colors[0], label=temp_axis_label)

you might also consider using numpy: Link if you are familiar with matlab and want to perform scientific computation with Python

Answered By: igon
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.