Skip to content

Instantly share code, notes, and snippets.

@robintux
Created December 3, 2025 20:50
Show Gist options
  • Select an option

  • Save robintux/53b0f36da0aabc243452153c674c9243 to your computer and use it in GitHub Desktop.

Select an option

Save robintux/53b0f36da0aabc243452153c674c9243 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import numpy as np
def visualize_color_palette_simple(colors_plotly, title="Paleta de Colores"):
"""
Visualización simple y elegante de una paleta de colores
"""
# Convertir a formato Matplotlib
colors_mpl = []
for color_str in colors_plotly:
if isinstance(color_str, str) and color_str.startswith('rgb'):
rgb = color_str[4:-1].split(',')
r, g, b = [int(x.strip())/255 for x in rgb]
colors_mpl.append((r, g, b))
else:
colors_mpl.append(color_str)
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))
# 1. Visualización de barras
n_colors = len(colors_mpl)
for i, color in enumerate(colors_mpl):
ax1.bar(i, 1, color=color, width=0.8, edgecolor='black', linewidth=2)
# Añadir etiqueta con valor HEX
import matplotlib.colors as mcolors
hex_color = mcolors.to_hex(color)
ax1.text(i, 0.5, hex_color.upper(),
ha='center', va='center',
fontsize=10, fontweight='bold',
color='white' if sum(color[:3])/3 < 0.5 else 'black')
ax1.set_xlim(-0.5, n_colors - 0.5)
ax1.set_ylim(0, 1.2)
ax1.set_xticks(range(n_colors))
ax1.set_xticklabels([f'C{i}' for i in range(n_colors)])
ax1.set_title(f'{title} - {n_colors} colores', fontsize=14, fontweight='bold')
ax1.set_ylabel('Intensidad')
ax1.grid(True, alpha=0.3, axis='y')
# 2. Información detallada
ax2.axis('off')
info_text = []
for i, (color_str, color_mpl) in enumerate(zip(colors_plotly, colors_mpl)):
rgb_values = color_str.replace('rgb(', '').replace(')', '')
hex_color = mcolors.to_hex(color_mpl)
info_text.append(
f"Color {i}: "
f"RGB({rgb_values}) | "
f"HEX: {hex_color.upper()}"
)
ax2.text(0.02, 0.95, '\n'.join(info_text),
transform=ax2.transAxes,
fontfamily='monospace',
fontsize=10,
verticalalignment='top',
bbox=dict(boxstyle='round', facecolor='whitesmoke', alpha=0.8))
plt.tight_layout()
return fig
# Usar
fig = visualize_color_palette_simple(
['rgb(255, 255, 217)', 'rgb(151, 214, 185)',
'rgb(31, 128, 184)', 'rgb(8, 29, 88)'],
title="Paleta YlGnBu (simplificada)"
)
# fig = visualize_color_palette_simple(
# ['rgb(255, 255, 217)', 'rgb(224, 243, 178)', 'rgb(151, 214, 185)', 'rgb(65, 182, 196)', 'rgb(31, 128, 184)', 'rgb(36, 66, 155)', 'rgb(8, 29, 88)'],
# title="Paleta YlGnBu (simplificada)"
# )
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment