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?
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.
drop the ,:
…
color = colors[0]
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
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?
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.
drop the ,:
…
color = colors[0]
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