Cómo agregar dos niveles de etiquetas para un gráfico de barras agrupadas y apiladas en matplotlib.
Estoy tratando de agregar dos niveles de etiquetas a este gráfico de barras apiladas agrupadas usando matplotlib. Cada barra en cada posición respectiva de cada grupo debería tener la misma etiqueta (es decir, la primera barra de cada grupo debería etiquetarse como “1”, la segunda barra como “2”, etc.). Luego, me gustaría tener un segundo nivel de etiquetas para cada grupo. Hasta ahora, esto es lo que tengo:
width = 0.25
x = np.arange(1, 7)
fig = plt.figure(figsize=(10,6))
ax = plt.axes()
ax.bar(x-0.4, shift1_rbc, width, color='red', tick_label='1')
ax.bar(x-0.1, shift2_rbc, width, color='red')
ax.bar(x+0.2, shift3_rbc, width, color='red')
ax.bar(x-0.4, shift1_plt, width*.7, color='blue')
ax.bar(x-0.1, shift2_plt, width*.7, color='blue')
ax.bar(x+0.2, shift3_plt, width*.7, color='blue')
ax.bar(x-0.4, shift1_ffp, width*.5, color='green')
ax.bar(x-0.1, shift2_ffp, width*.5, color='green')
ax.bar(x+0.2, shift3_ffp, width*.5, color='green')
Cuando intento agregar el parámetro “tick_label” a otro conjunto de barras, reemplaza la etiqueta anterior, como se muestra a continuación:
width = 0.25
x = np.arange(1, 7)
fig = plt.figure(figsize=(10,6))
ax = plt.axes()
ax.bar(x-0.4, shift1_rbc, width, color='red', tick_label='1')
ax.bar(x-0.1, shift2_rbc, width, color='red', tick_label='1')
ax.bar(x+0.2, shift3_rbc, width, color='red')
ax.bar(x-0.4, shift1_plt, width*.7, color='blue')
ax.bar(x-0.1, shift2_plt, width*.7, color='blue')
ax.bar(x+0.2, shift3_plt, width*.7, color='blue')
ax.bar(x-0.4, shift1_ffp, width*.5, color='green')
ax.bar(x-0.1, shift2_ffp, width*.5, color='green')
ax.bar(x+0.2, shift3_ffp, width*.5, color='green')
Agradezco cualquier ayuda que puedan brindar.
davy.ai
Un enfoque para agregar dos niveles de etiquetas al gráfico de barras agrupadas apiladas es utilizar la función
xticks
para establecer las ubicaciones y texto de las etiquetas del eje x. Aquí tienes un ejemplo de código:Este código produce un gráfico de barras agrupadas apiladas con dos niveles de etiquetas, donde cada grupo de barras tiene una etiqueta para cada posición de la barra, y también una etiqueta para cada turno. La salida del gráfico debería lucir así:
¡Espero que esta solución sea de ayuda! Hazme saber si tienes alguna pregunta.