简体   繁体   中英

Modify the range of values ​of the color bar of a graph in python

I have the following issue.

I have a graph of which has colored segments. The problem is in relating those segments to the color bar (which also contains text), so that each color segment is aligned with the color bar.

The code is the following:

from matplotlib.colorbar import colorbar_factory

x_v = datosg["Hour"]+div
y_v = datosg["UV Index"]

fig, ax= plt.subplots(figsize = (7,7))

ax.plot(x_v, y_v, color = "green")
ax.set_xlim(7, 19)
ax.grid()
ax.axhspan(0, 2.5, facecolor='green', alpha=0.8)
ax.axhspan(2.5, 5.5, facecolor='blue', alpha=0.7)
ax.axhspan(5.5, 7.5, facecolor='red', alpha=0.7)
ax.axhspan(7.5, 10.5, facecolor='yellow', alpha=0.7)
ax.axhspan(10.5, 16, facecolor='pink', alpha=0.7)
ax.margins(0)

from matplotlib.colors import ListedColormap

#discrete color scheme

cMap = ListedColormap(['green', 'blue','red', 'yellow', 'pink'])

#data
np.random.seed(42)
data = np.random.rand(5, 5)
heatmap = ax.pcolor(data, cmap=cMap)

#legend

cbar_ay = fig.add_axes([0.93, 0.125, 0.2, 0.755])
cbar = plt.colorbar(heatmap, cax=cbar_ay, orientation="vertical")

cbar.ax.get_yaxis().set_ticks([])
for j, lab in enumerate(['$Bajo$','$Medio$','$Alto$','$Muy Alto$','$Extremo$']):
    cbar.ax.text(.5, (2 * j + 1) / 10.0, lab, ha='center', va='center')

plt.show()

The graph that results from this code is as follows: Result_code

I have tried everything, the result I expect is very similar to this graph: resulting image

But I can't change the range of the colors in the color bar.

Also note that I created random values in order to create the colorbar, I couldn't think of any other way, however so far it has worked. I only have to modify the range, so that it is similar to the last graph.

Any help would be appreciated.

I guess it's much easier to just draw a second Axes and fill it with axhspan s the same way you did it with the main Axes, but if you want to use a colorbar, you can do it as follows:

import itertools
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

colors = ['green', 'blue','red', 'yellow', 'pink']
labels = ['$Bajo$','$Medio$','$Alto$','$Muy Alto$','$Extremo$']
bounds = np.array([0, 2.5, 5.5, 7.5, 10.5, 16 ])

fig, ax= plt.subplots()

for span, color in zip(itertools.pairwise(bounds), colors):
    ax.axhspan(*span, facecolor=color, alpha=0.8)
ax.margins(0)

cmap = mpl.colors.ListedColormap(colors)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

ax_pos = ax.get_position().bounds
cbar_ay = fig.add_axes([0.93, ax_pos[1], 0.2, ax_pos[3]])
cbar = plt.colorbar(mpl.cm.ScalarMappable(cmap=cmap, norm=norm), cax=cbar_ay, orientation="vertical", spacing='proportional')
cbar.ax.set_axis_off()

for y, lab in zip(bounds[:-1] + np.diff(bounds) / 2, labels):
    cbar.ax.text(.5, y, lab, ha='center', va='center')

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM