Formatting tick marks

125 Views Asked by At

I am trying to make my column density plot axes look like the ones on the image presented so that I can present my work alongside a previous work using similar format. I am struggling to format my ticks in the same way you can see in the image. Firstly, I need a way to make my tick marks and axis grid be white while my ticks numbers are still black and secondly, I want every second tick to be larger than the others. Can anyone help me with these two issues?

I am aware of plt.xticks or the equivalent ax.tick_params, as well as how to change the colour of the axis grid, but from my understanding I can only change the colour of the numbers AND the tick marks on the plot using these, not just the second. What if I just want to change the marks?This is the result I am trying to achieve

2

There are 2 best solutions below

0
matleg On

I took an example from here that I adapted:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import AutoMinorLocator, MultipleLocator

t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)
fig, ax = plt.subplots()
ax.plot(t, s)

ax.xaxis.set_major_locator(MultipleLocator(20))
ax.xaxis.set_major_formatter("{x:.0f}")

# For the minor ticks, use no labels; default NullFormatter.
ax.xaxis.set_minor_locator(MultipleLocator(5))

ax.tick_params(which="both", color="white", direction="in")
ax.set_facecolor("black")
plt.show()

Seems you'll be able to change the mark size, the color, and the direction, while keeping the text black:

axis example

3
paime On

I defined a stylesheet that mimic your plot style:

image.mplstyle:

# Image
image.cmap: gist_heat
# TeX
font.family: Helvetica
text.usetex: true
# Labels
axes.labelsize: x-large
# XAXis
xtick.bottom: true
xtick.color: white
xtick.direction: in
xtick.labelcolor: black
xtick.labelsize: x-large
xtick.major.size: 10
xtick.minor.size: 5
xtick.minor.visible: true
xtick.top: true
# YAXis
ytick.color: white
ytick.direction: in
ytick.labelcolor: black
ytick.labelsize: x-large
ytick.left: true
ytick.major.size: 10
ytick.minor.size: 5
ytick.minor.visible: true
ytick.right: true

It will be slightly wrong for the colorbar that is an ax per itself, so need a little mplstyle for the colorbar:

cbar_fix.mplstyle:

ytick.color: black
ytick.major.size: 5
ytick.minor.size: 2.5

To be used like that:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator

# DATA
xmin, xmax = -0.5, 0.5
ymin, ymax = -0.5, 0.5
npoints = 256

x = np.linspace(xmin, xmax, npoints)
y = np.linspace(ymin, ymax, npoints)

fwhm = 0.4
arr = np.exp(-4 * np.log(2) * (x**2 + y[:, None] ** 2) / fwhm**2)

# PLOT
with plt.style.context("./image.mplstyle"):
    fig, ax = plt.subplots()

    im = ax.imshow(arr, extent=[xmin, xmax, ymin, ymax])

    ax.set_xlabel("x [pc]")
    ax.set_ylabel("y [pc]")

    ax.xaxis.set_minor_locator(AutoMinorLocator(n=2))
    ax.yaxis.set_minor_locator(AutoMinorLocator(n=2))

    with plt.style.context(["./image.mplstyle", "./cbar_fix.mplstyle"]):
        cbar = fig.colorbar(im, ax=ax)
        cbar.ax.yaxis.set_minor_locator(AutoMinorLocator(n=2))
        # Still need that, idk why
        cbar.ax.tick_params(which="both", left=True)

plt.show()

Styled image