Matplotlib Quiver plot matching key label color with arrow color

此生再无相见时 提交于 2021-02-11 13:47:47

问题


Using matplotlib, python3.6. I am trying to create some quiverkeys for a quiver plot but having a hard time getting the label colors to match certain arrows. Below is a simplified version of the code to show the issue. When I use the same color (0.3, 0.1, 0.2, 1.0 ) for a vector at (1,1) and as 'labelcolor' of a quiverkey I see 2 different colors.

q=plt.quiver([1, 2,], [1, 1],
             [[49],[49]],
             [0],
             [[(0.6, 0.8, 0.5, 1.0 )],
             [(0.3, 0.1, 0.2, 1.0 )]],
             angles=[[45],[90]])
plt.quiverkey(q, .5, .5, 7, r'vector2', labelcolor=(0.3, 0.1, .2, 1),
              labelpos='S', coordinates = 'figure')


回答1:


Supposedly you meant to be using the color argument of quiver to set the actual colors.

import matplotlib.pyplot as plt

q=plt.quiver([1, 2,], [1, 1], [5,0], [5,5],
             color=[(0.6, 0.8, 0.5, 1.0 ), (0.3, 0.1, 0.2, 1.0 )])
plt.quiverkey(q, .5, .5, 7, r'vector2', labelcolor=(0.3, 0.1, .2, 1),
                      labelpos='S', coordinates = 'figure')

plt.show()

Else, the C argument is interpreted as the values to map to colors according to the default colormap. Since you only have two arrows, only the first two values from the 8 numbers in the array given to the C argument are taken into account. But the colormap normalization uses all of those values, such that it ranges between 0.1 and 1.0. The call

q=plt.quiver([1, 2,], [1, 1], [5,0], [5,5],
             [(0.6, 0.8, 0.5, 1.0 ), (0.3, 0.1, 0.2, 1.0 )])

is hence equivalent to

q=plt.quiver([1, 2,], [1, 1], [5,0], [5,5],
             [0.6, 0.8], norm=plt.Normalize(vmin=0.1, vmax=1))

resulting in the first arrows color to be the value of 0.6 in the viridis colormap normalized between 0.1 and 1.0, and the second arrow to 0.8 in that colormap.

This becomes apparent if we add plt.colorbar(q, orientation="horizontal"):



来源:https://stackoverflow.com/questions/55871552/matplotlib-quiver-plot-matching-key-label-color-with-arrow-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!