Given (255,0,255)
, how can I output a colorwheel where this value is marked?
Given (255,0,255)
, how can I output a colorwheel where this value is marked?
You can use the below code to achieve your purpose.
Explanation:
I have defined the plot_color_marker
function, which takes an RGB color as input:
mpl.colors.rgb_to_hsv
.2 * π
.marker='s'
) with the specified size (markersize=5
), color (color=rgb_color
), border color (markeredgecolor='yellow'
), and border width (markeredgewidth=2
) at the calculated angle and a fixed radius (0.5) on the polar plot.Next step is to create a polar plot by adding a new set of axes to the figure with projection='polar'
. The _direction
attribute is set to 2 * π
to make the plot go clockwise.
Then created a color normalization object to map values from 0 to 2 * π.
Create a color wheel using the HSV color map with 2056 quantization steps and the previously defined normalization object. The color wheel is plotted as a horizontal colorbar on the polar plot.
I created a color normalization object to map values from 0 to 2 * π.
Next step was to create a color wheel using the HSV color map with 2056 quantization steps and the previously defined normalization object. The color wheel is plotted as a horizontal colorbar on the polar plot.
Then I hid the colorbar's outline and the polar plot's axis labels.
Defined the user-provided RGB color as a tuple (in this case, (255, 0, 255)
). You can try modifying this as per your needs.
Normalized the user-provided RGB color by dividing each component by 255.
Call the plot_color_marker
function with the normalized RGB color to plot the marker on the color wheel.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
import matplotlib as mpldef plot_color_marker(rgb_color):hsv_color = mpl.colors.rgb_to_hsv(rgb_color)angle = hsv_color[0] * 2 * np.pidisplay_axes.plot(angle, 0.5, marker='s', markersize=5, color=rgb_color, markeredgecolor='yellow', markeredgewidth=2)fig = plt.figure()display_axes = fig.add_axes([0.1,0.1,0.8,0.8], projection='polar')
display_axes._direction = 2*np.pinorm = mpl.colors.Normalize(0.0, 2*np.pi)quant_steps = 2056
cb = mpl.colorbar.ColorbarBase(display_axes, cmap=cm.get_cmap('hsv',quant_steps),norm=norm,orientation='horizontal')cb.outline.set_visible(False)
display_axes.set_axis_off()user_rgb_color = (255, 0, 255) # Provide the RGB color here. You can modify it as per your usecase.
normalized_rgb_color = tuple(x / 255.0 for x in user_rgb_color)
plot_color_marker(normalized_rgb_color)plt.show()
Code demo
References: