Ep1c1aN
Ep1c1aN

Reputation: 733

Setting up an axis as colorbar

I have a variable, let's call it Ecozone (Blue scatter). I want to plot them in the same plot. For Ecozone, I want to try something a bit more complex. Rather than the scatter, I want to have a colour bar (position of colour bar: along the x-axis, somewhere between -5 to 0 [left y-axis]). And each unique number represents a different colour. To ease the process I have the array and colour pasted below:

>>Ecozone.shape
Output: (169,)
>>Ecozone_unique = np.unique(Ecozone)[~np.isnan(np.unique(Ecozone))]
>>Ecozone_unique
Output: array([ 85., 101., 228., 349., 402., 417., 448., 461., 576., 636., 722.,
       756., 798., 831.])
color = ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c', '#fdbf6f', 
         '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928', '#8c510a', '#bf812d']
# Thus one color for each individual value
color_sel = []
for value in Ecozone:
    for i in range(0,Ecozone_unique.shape[0]):
        if value == Ecozone_unique[i]:
            color_sel.append(color[i])
        else:
            continue

I arranged each value as a separate colour in the color_sel. (Code for color_sel can be provided if needed)

#Below is the code for colorbar
import matplotlib as mpl
mpl.rcParams.update({'font.size': 15})
hfont = {'fontname':'Arial'}
fig=plt.figure(figsize=(15,3))
ax=fig.add_subplot(111)
vals=Ecozone_unique
cmap = mpl.colors.ListedColormap(np.array(color_sel))
cb = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
                                spacing='uniform',
                                orientation='horizontal',
                                extend='neither',
                                ticks=vals)

cb.set_label('Ecozones', **hfont)
ax.set_position((0.1, 0.45, 0.8, 0.1))

enter image description here I want to put this colour bar in my final plot. With some code and power point skills, my final plot should like below.

fig, ax1 = plt.subplots(figsize = (15,6))
ax2 = ax1.twinx() 
ax1.scatter(np.linspace(0,S_r_yhat_BESS.shape[0]-1,S_r_yhat_BESS.shape[0]+1), Ecozone)
ax1.set_ylim(-50,900)

enter image description here

Upvotes: 0

Views: 113

Answers (1)

JohanC
JohanC

Reputation: 80279

Matplotlib's barh could be used to draw the colorbar on the desired positions. color_sel could be created via a dictionary lookup.

import numpy as np
from matplotlib import pyplot as plt

Ecozone_unique = np.array([85., 101., 228., 349., 402., 417., 448., 461., 576., 636., 722., 756., 798., 831.])
Ecozone = np.random.choice(Ecozone_unique, 169)
color = ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c', '#fdbf6f',
         '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928', '#8c510a', '#bf812d']
color_dict = {zone: col for zone, col in zip(Ecozone_unique, color)}
color_sel = [color_dict[zone] for zone in Ecozone]

fig, ax1 = plt.subplots(figsize=(15, 6))
# ax1.scatter(np.linspace(0,S_r_yhat_BESS.shape[0]-1,S_r_yhat_BESS.shape[0]+1), Ecozone)
ax1.scatter(np.linspace(0, len(Ecozone), 100), np.random.randint(1, 17, 100) * 50)
ax1.set_xlim(0, len(Ecozone))
ax1.set_ylim(-50, 900)
ax1.barh(0, height=-50, align='edge', width=1, left=np.arange(0, len(Ecozone)), color=color_sel)

plt.show()

resulting plot

Upvotes: 1

Related Questions