pythonmatplotlibcontourf

Is there a way to display tick marks inside a contourf plot?


I am trying to display the tick marks inside the axis for a contourf plot.

When the call ax1.contourf() is made, the contour plots overtop and removes the tick marks. Is there any way to stop this from happening or to add the tick marks back in? As shown in the image the ticks are on the axis correctly before this call is made.

I have tried manually specifying the ticks with ax1.set_xticks but this didn't make the ticks display and also tried setting visible=True.

For clarity, the number labels themselves are fine and I can change them, I just want the actual tick line to also be in the plot.

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10,4))
ax1 = fig.add_subplot([0.15, 0.15, 0.35, 0.73])

r = np.linspace(0.5, 3, 100)
z = np.linspace(0, 2*np.pi, 100)

data = [[r[i]*z[j] for i in range(np.size(r))] for j in range(np.size(z))]

R1, Z = np.meshgrid(r, z)

cont = ax1.contourf(R1,Z, data, 500, cmap = 'coolwarm')

plt.show()

This feels like it should be a simple fix and I'm just missing something easy, but I just can't work this out.

Axis with correct tick marks and contour without marks

EDIT: I am calling plt.style.use(['ggplot','mpl_latex_stylesheet']) which is likely causing my issue.

Here is my Matplotlib style sheet:

font.size : 15         # controls default text sizes
axes.titlesize : 15    # fontsize of the axes title
axes.labelsize : 15    # fontsize of the x and y labels
xtick.labelsize : 14   # fontsize of the tick labels
ytick.labelsize : 14   # fontsize of the tick labels
legend.fontsize : 14   # legend fontsize
figure.titlesize : 15  # fontsize of the figure title
axes.facecolor: None
axes.edgecolor: black
xtick.color: black
ytick.color: black
text.color: black
axes.labelcolor: black
xtick.direction: "in"
ytick.direction: "in"
axes.grid: False

text.usetex : True
text.latex.preamble: "\usepackage{amssymb,amsmath}"
font.family: sans-serif,
font.sans-serif: cm
savefig.bbox: tight
savefig.format: pdf

Solution

  • You can add ax1.tick_params(axis="x", direction="in")

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure(figsize=(10,4))
    ax1 = fig.add_subplot([0.15, 0.15, 0.35, 0.73])
    
    r = np.linspace(0.5, 3, 100)
    z = np.linspace(0, 2*np.pi, 100)
    
    data = [[r[i]*z[j] for i in range(np.size(r))] for j in range(np.size(z))]
    
    R1, Z = np.meshgrid(r, z)
    
    cont = ax1.contourf(R1,Z, data, 500, cmap = 'coolwarm')
    
    ax1.tick_params(axis="x", direction="in")
    
    plt.show()
    

    Add x tick marks inside the axis of the contourf plot