0

I have a grid and in each cell of the grid I plot circles where the circle has a color that is linked to a colorbar.

However colorbar is shown automatically in the right hand side of the plot.

How to control the colorbar and show it below the circles as horizontal?

The code below generates the given figure.

import matplotlib
import numpy as np
import os
import matplotlib as mpl
from matplotlib.colors import Normalize
import matplotlib.cm as matplotlib_cm
from matplotlib import pyplot as plt


jobname="DEBUG"
row_list=['row1', 'row2', 'row3']
column_list=[2]
maxProcessiveGroupLength=2
index = column_list.index(maxProcessiveGroupLength)

plot1, panel1 = plt.subplots(figsize=(20+1.5*len(column_list), 10+1.5*len(row_list)))
plt.rc('axes', edgecolor='lightgray')

#make aspect ratio square
panel1.set_aspect(1.0)
panel1.text(0.1, 1.2, jobname,horizontalalignment='center', verticalalignment='top', fontsize=60, fontweight='bold', fontname='Arial',transform=panel1.transAxes)

if (len(column_list) > 1):
    panel1.set_xlim([1, index + 1])
    panel1.set_xticks(np.arange(0, index + 2, 1))
else:
    panel1.set_xlim([0, len(column_list)])
    panel1.set_xticks(np.arange(0, len(column_list)+1, 1))

if (len(row_list) > 1):
    panel1.set_ylim([1, len(row_list)])
else:
    panel1.set_ylim([0, len(row_list)])

panel1.set_yticks(np.arange(0, len(row_list) + 1, 1))

panel1.set_facecolor('white')
panel1.grid(color='black')

for edge, spine in panel1.spines.items():
    spine.set_visible(True)
    spine.set_color('black')

xlabels = None
if (index is not None):
    xlabels = column_list[0:index + 1]
ylabels = row_list

cmap = matplotlib_cm.get_cmap('Blues')  # Looks better
v_min = 2
v_max = 20
norm = Normalize(v_min, v_max)
bounds = np.arange(v_min, v_max+1, 2)

# Plot the circles with color
for row_index, row in enumerate(row_list):
    for column_index, processive_group_length in enumerate(column_list):
        radius=0.35
        color=10+column_index*3+row_index*3
        circle = plt.Circle((column_index + 0.5, row_index + 0.5), radius,color=cmap(norm(color)), fill=True)
        panel1.add_patch(circle)

# Vertical Colorbar
# Used for scatter plot
x = []
y = []
c = []

for row_index, processiveGroupLength in enumerate(row_list):
    x.append(row_index)
    y.append(row_index)
    c.append(0.5)

# This code defines the ticks on the color bar
# plot the scatter plot
sc = plt.scatter(x, y, s=0, c=c, cmap=cmap, vmin=v_min, vmax=v_max, edgecolors='black')
cb = plt.colorbar(sc)  # this works because of the scatter

cb.ax.set_ylabel("colorbar label", fontsize=50, labelpad=25)

# common for horizontal colorbar and vertical colorbar
cbax = cb.ax
cbax.tick_params(labelsize=40)
text_x = cbax.xaxis.label
text_y = cbax.yaxis.label
font = mpl.font_manager.FontProperties(size=40)
text_x.set_font_properties(font)
text_y.set_font_properties(font)

# CODE GOES HERE TO CENTER X-AXIS LABELS...
panel1.set_xticklabels([])
mticks = panel1.get_xticks()
panel1.set_xticks((mticks[:-1] + mticks[1:]) / 2, minor=True)
panel1.tick_params(axis='x', which='minor', length=0, labelsize=50)

if xlabels is not None:
    panel1.set_xticklabels(xlabels,minor=True)

panel1.xaxis.set_ticks_position('top')

plt.tick_params(
    axis='x',  # changes apply to the x-axis
    which='major',  # both major and minor ticks are affected
    bottom=False,  # ticks along the bottom edge are off
    top=False)  # labels along the bottom edge are off


# CODE GOES HERE TO CENTER Y-AXIS LABELS...
panel1.set_yticklabels([])
mticks = panel1.get_yticks()
panel1.set_yticks((mticks[:-1] + mticks[1:]) / 2, minor=True)
panel1.tick_params(axis='y', which='minor', length=0, labelsize=50)
panel1.set_yticklabels(ylabels, minor=True)  # fontsize

plt.tick_params(
    axis='y',  # changes apply to the x-axis
    which='major',  # both major and minor ticks are affected
    left=False)  # labels along the bottom edge are off


plt.show()

enter image description here

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
burcak
  • 1,009
  • 10
  • 34
  • It took me < 30 sec to paste your title into Google and find a duplicate. Please do your research before posting. – Mad Physicist May 02 '21 at 15:22
  • I couldn't apply that code to solve my problem. – burcak May 02 '21 at 15:24
  • That's a failure on your part and possibly worth another question. Just remember that if you expect to do copy and paste without thought, you probably won't go very far – Mad Physicist May 02 '21 at 15:26
  • I didn't copy and paste. I need an image object to attach the colorbar to. I wanted to learn how I can get that image. If it makes sense on your side. I was aiming to solve it using subplots option in https://stackoverflow.com/questions/13310594/positioning-the-colorbar – burcak May 02 '21 at 15:49
  • https://matplotlib.org/stable/gallery/subplots_axes_and_figures/colorbar_placement.html – Jody Klymak May 02 '21 at 19:51
  • @JodyKlymak I don't have pcolormesh. I'm adding circles. In my case, I 'm using plot.scatter, dummy "sc" to attach colorbar to. How can I get rid of it? – burcak May 02 '21 at 20:45
  • I don't think placing the colorbar has anything to do with what kind of artist you are mapping. But maybe I am misunderstanding your question. – Jody Klymak May 03 '21 at 01:45
  • @JodyKlymak How can I add colorbar without using dummy "sc"? since plt.colorbar() requires an object mappable. Is my question clear? – burcak May 03 '21 at 05:55
  • Your question above is how to make the colorbar horizontal. Are you now asking a separate question? – Jody Klymak May 03 '21 at 13:48

0 Answers0