Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Decreasing color bar major ticks size

I just started implementing rc Parametrization with matplotlib to clean up productivity when it comes to plotting data.

When playing with the parameter of the axis ticks, it effects my colorbar tick size, making it look goofy.

I did not include the calculations i've made, just only part of the script that plots the data.

import matplotlib.pyplot as plt
import numpy as np

#=================================
##### Custom Plot Parameters #####
#=================================
params = {
    'backend': 'wxAgg',
    'lines.markersize': 2,
    'lines.linewidth': 2.5,
    'axes.labelsize': 16,
    'axes.linewidth': 2.5,
    'xtick.major.size': 10,
    'xtick.minor.size': 3,
    'xtick.major.width': 2.5,
    'xtick.minor.width': 1.25,
    'ytick.major.size': 10,
    'ytick.minor.size': 3,
    'ytick.major.width': 2.5,
    'ytick.minor.width': 1.25,
    'xtick.labelsize': 15,
    'ytick.labelsize': 15,
    'text.fontsize': 10,
    'font.weight': 'heavy',
    'text.usetex': True, 
    'figure.figsize': [9,7],
    'legend.fontsize': 15,
    'legend.frameon': True,
}
plt.rcParams.update(params)


cmap = plt.get_cmap("Set1", 150)


##### ColorMap #####
cax = plt.scatter(RelDist, RadVel, c=Infall, cmap=cmap, vmin=0.1, vmax=max(Infall))
cbar = plt.colorbar(cax, ticks=[0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0], format=None, shrink=0.7, pad=0.01)


plt.plot(R, nfwEsc08, ':', linewidth=2., color='black', label=r'$ 0.8 \times 10^{12}$')
plt.plot(R, nfwEsc12, '-.', linewidth=2., color='black', label=r'$ 1.2 \times 10^{12}$')
plt.plot(R, nfwEsc20, '--', linewidth=2., color='black', label=r'$ 2.0 \times 10^{12}$')

plt.plot(R, -nfwEsc08, ':', linewidth=2., color='black')
plt.plot(R, -nfwEsc12, '-.', linewidth=2., color='black')
plt.plot(R, -nfwEsc20, '--', linewidth=2., color='black')

plt.xlim(0,1400)
plt.ylim(-450,450)

plt.xlabel('$\mathrm{Galactocentric\ Distance}$ [$\mathrm{kpc}$]')
plt.ylabel('$\mathrm{Radial\ Velocity}$ [$\mathrm{km}\ \mathrm{s}^{-1}$]')
cbar.set_label('$\mathrm{Infall\ Time}$ [$\mathrm{Gyr}$]')




plt.legend()



plt.show()

enter image description here

I have been having trouble finding documentation for matplotlib.rcParams that involved anything with a colorbar. I their is any.

How do adjust the major tick sizing for the colorbar that does not effect the tick sizing for my plot axis?

------------- Edit ---------------

#=================================
##### Custom Plot Parameters #####
#=================================
params = {
    'backend': 'wxAgg',
    'lines.markersize': 2,
    'lines.linewidth': 2.5,
    'axes.labelsize': 16,
    'axes.linewidth': 2.5,
    'xtick.major.size': 10,
    'xtick.minor.size': 3,
    'xtick.major.width': 2.15,
    'xtick.minor.width': 1.25,
    'ytick.major.size': 10,
    'ytick.minor.size': 3,
    'ytick.major.width': 2.15,
    'ytick.minor.width': 1.25,
    'xtick.labelsize': 15,
    'ytick.labelsize': 15,
    'text.fontsize': 10,
    'font.weight': 'heavy',
    'text.usetex': True, 
    'figure.figsize': [9,7],
    'legend.fontsize': 15,
    'legend.frameon': True,
}
plt.rcParams.update(params)


x = np.array(np.random.randint(0, 50, 500))
y = x**2
z = y/x

cmap = plt.get_cmap("Set1", 150)


##### ColorMap #####
cax = plt.scatter(x, y, c=z, cmap=cmap, vmin=0.1, vmax=max(z))
cbar = plt.colorbar(cax, format=None, shrink=0.7, pad=0.01)

plt.legend()

plt.show()

enter image description here

The problem is that the customization of major axis is also effecting the major ticks in the color bar, causing is to thicken profusely.

What is a way to somehow not let that happen for the tick in the colorbar, but somehow lets me still preserve the alteration for my plot?

like image 341
iron2man Avatar asked Apr 17 '26 04:04

iron2man


2 Answers

With the rcParams you are editing the default values for the upcoming plots. What you need to know is that Colormap cbar has its own ax attribute which you can access with cbar.ax (docs). With that knowledge, you can change the ticks of that particular axis after its creation while preserving your defaults:

[...]
cbar = plt.colorbar(cax, format=None, shrink=0.7, pad=0.01)
cbar.ax.tick_params(labelsize=5, width=2)
like image 61
dummman Avatar answered Apr 18 '26 17:04

dummman


You can adjust size of major and minor ticks using this reference. https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.tick_params.html You can change lenght and width = size. For example:

cbar.ax.tick_params(labelsize=40,length=10, width=4, which="major")

This makes labels of my colobar larger, and major ticks are longer and wider.

like image 39
Ondrej_D Avatar answered Apr 18 '26 18:04

Ondrej_D