matplotlib 刻度线

w8f9ii69  于 2023-10-24  发布在  其他
关注(0)|答案(2)|浏览(142)

我试图让我的列密度图轴看起来像图像上的那些,这样我就可以用类似的格式与以前的作品一起展示我的作品。我正在努力以你在图像中看到的相同方式格式化我的刻度。首先,我需要一种方法来让我的刻度线和轴网格是白色,而我的刻度数字仍然是黑色,其次,我希望每一秒的刻度都比其他的大。有人能帮我解决这两个问题吗?
我知道plt.xticks或等效的ax.tick_params,以及如何更改轴网格的颜色,但据我所知,我只能使用这些更改图上数字和刻度线的颜色,而不仅仅是第二个。如果我只是想更改标记怎么办?

vsnjm48y

vsnjm48y1#

我定义了一个样式表来模仿你的plot样式:
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

这将是稍微错误的colorbar是一个斧头每本身,所以需要一个小mplstyle的colorbar:
cbar_fix.mplstyle

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

像这样使用:

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()

332nm8kg

332nm8kg2#

我从here中引用了一个例子:

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()

似乎你可以改变标记的大小,颜色和方向,同时保持文本为黑色:

相关问题