我使用以下代码在 Jupyter 中生成图表:
import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
from scipy.integrate import quad
import math
from matplotlib.patches import Circle
fig2 = plt.figure(figsize=(10,8))
plt.subplot(1, 1, 1)
plt.plot(2, 5, marker='*', markersize=25, color='gold')
plt.plot(2, -5, marker='*', markersize=25, color='gold')
plt.plot(3, 0, marker='*', markersize=25, color='gold')
plt.xlabel(r"$x [rg]$", fontsize=20)
plt.ylabel(r"$y [rg]$", fontsize=20)
circle = Circle((0, 0), 1, color='black')
plt.gca().add_patch(circle)
plt.gca().set_aspect('equal')
plt.axis([-10, 30, -10, 10])
plt.tick_params(axis='both', labelsize=20)
但是,我希望它类似于下图的特征。使用 LaTeX 中用于文本和数字的相同默认字体,并在绘图的所有四个边上使用小“破折号”网格。我怎样才能实现这个目标?
Matplotlib 关于“使用 LaTeX 进行文本渲染”的文档中总是有这样的内容:
“Matplotlib 可以使用 LaTeX 渲染文本。这可以通过在 rcParams 中设置我没有采取那条路,但您可能想将其与下面的一些方法结合起来。
text.usetex : True
来激活,或者通过在单个 Text 对象上将usetex
属性设置为 True 来激活。”
usetex
的情况下接近,但在绘图标签中使用 Latex
这让你很接近,但你对你真正想要的一些东西很模糊,所以你需要定制更多:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import sympy as sp
from scipy.integrate import quad
import math
from matplotlib.patches import Circle
matplotlib.rcParams['font.family'] = ['serif'] # based on https://matplotlib.org/stable/tutorials/text/text_props.html
fig2 = plt.figure(figsize=(10,8))
plt.subplot(1, 1, 1)
plt.plot(2, 5, marker='*', markersize=25, color='gold')
plt.plot(2, -5, marker='*', markersize=25, color='gold')
plt.plot(3, 0, marker='*', markersize=25, color='gold')
#plt.xlabel(r"$x [rg]$", fontsize=20)
#plt.ylabel(r"$y [rg]$", fontsize=20)
circle = Circle((0, 0), 1, color='black')
plt.gca().add_patch(circle)
plt.gca().set_aspect('equal')
plt.axis([-10, 30, -10, 10])
plt.gca().set_ylabel(r'x[rg]' "\n" r'$x[rg]$' "\n" r'$r \slash r_S$',
fontsize=20, rotation = 0, labelpad=35) # rotation based on https://stackabuse.com/rotate-axis-labels-in-matplotlib/
# multiline labels based on https://stackoverflow.com/a/2666270/8508004 (just added for development of example because OP unclear on actual label)
# use of `labelpad` is to complement `set_label_position` use, see next line
plt.gca().yaxis.set_label_position("right") # label on right based on https://stackoverflow.com/a/64363955/8508004
plt.gca().set_xlabel(r"$y [rg]$", fontsize=20, rotation = 0)
plt.gca().minorticks_on() # based on https://stackoverflow.com/a/57280702/8508004
plt.gca().yaxis.set_ticks_position('both')# Ticks on all 4 sides; based on https://stackoverflow.com/a/56577545/8508004
plt.gca().xaxis.set_ticks_position('both') # based on https://stackoverflow.com/a/56577545/8508004
plt.gca().xaxis.set_tick_params(which='both', direction = 'in')# ticks facing inward direction based on https://stackoverflow.com/q/71366709/8508004
plt.gca().yaxis.set_tick_params(which='both', direction = 'in')# ticks facing inward based on https://stackoverflow.com/q/71366709/8508004
plt.tick_params(axis='both', labelsize=20)
mpl.rcParams["text.usetex"] = True
和正确的乳胶安装。对于刻度格式,您需要设置 函数的参数。
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
mpl.rcParams["text.usetex"] = True
fig, ax = plt.subplots(figsize=(10, 8))
ax.plot(2, 5, marker="*", markersize=25, color="gold")
ax.plot(2, -5, marker="*", markersize=25, color="gold")
ax.plot(3, 0, marker="*", markersize=25, color="gold")
circle = Circle((0, 0), 1, color="black")
ax.add_patch(circle)
ax.set_xlabel(r"$x [rg]$", fontsize=20)
ax.set_ylabel(r"$y [rg]$", fontsize=20)
ax.set_aspect("equal")
ax.set_xlim(-10, 30)
ax.set_ylim(-10, 10)
ax.tick_params(axis="both", top=True, right=True, labelsize=20, direction="in")
plt.show()
(检查您可以设置的属性) 样式表(例如“./my_latex_style.mplstyle”):
text.usetex : True
axes.labelsize : 20
xtick.bottom : True
xtick.top : True
xtick.direction : "in"
xtick.labelsize : 20
ytick.left : True
ytick.right : True
ytick.direction : "in"
ytick.labelsize : 20
用途:
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
plt.style.use("./my_latex_style.mplstyle")
fig, ax = plt.subplots(figsize=(10, 8))
ax.plot(2, 5, marker="*", markersize=25, color="gold")
ax.plot(2, -5, marker="*", markersize=25, color="gold")
ax.plot(3, 0, marker="*", markersize=25, color="gold")
circle = Circle((0, 0), 1, color="black")
ax.add_patch(circle)
ax.set_xlabel(r"$x [rg]$")
ax.set_ylabel(r"$y [rg]$")
ax.set_aspect("equal")
ax.set_xlim(-10, 30)
ax.set_ylim(-10, 10)
plt.show()
注意:样式表的相对导入仅在 3.7.0 中存在错误,已在 3.7.1 中修复,请参阅 issue #25242。