我有一个带有对数 X 轴的 matplotlib 图,我想将其保存为 pgf 并将其包含到我的乳胶文档中。在所需的大小上,matplotlib 不会在 X 轴上标记 base=10 的每一轮幂。另一方面,如果我增加绘图的大小,它们会与短轴一起显示。我找到了 LogLocator,但我无法说服它产生我想要实现的输出。
我使用的代码:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('pgf')
pgf_with_custom_preamble = {
"pgf.texsystem": "pdflatex",
"font.family": "serif", # use serif/main font for text elements
"text.usetex": True, # use inline math for ticks
"pgf.rcfonts": False, # don't setup fonts from rc parameters
"figure.figsize": (3.3914487339144874*0.5, 2.0960305886619515*0.8),
"axes.labelsize": 8,
"axes.grid": True,
"font.size": 7,
"legend.fontsize": 8,
"legend.handlelength": 2,
"legend.handletextpad": 0.4,
"legend.columnspacing": 1,
"xtick.labelsize": 8,
"ytick.labelsize": 8,
"xtick.direction":"in",
"ytick.direction":"in",
"xtick.major.size":1.5,
"ytick.major.size":1.5,
"grid.alpha": 0.6,
"lines.markersize": 4,
"savefig.pad_inches":0,
"savefig.bbox":"tight",
"savefig.dpi":300,
"pgf.preamble": r"\usepackage[detect-all,locale=US]{siunitx}\usepackage{amsmath}\usepackage[utf8x]{inputenc}\usepackage[T1]{fontenc}"
}
matplotlib.rcParams.update(pgf_with_custom_preamble)
freq = np.logspace(3,8,100)
gain = 50*np.random.random_sample(100)-20
fig, ax = plt.subplots()
ax.grid(True,which='minor', alpha=0.3, axis='both')
ax.grid(True,which='major', alpha=0.7, axis='both')
ax.set_xscale('log')
ax.tick_params(axis='both', which='major', pad=2)
ax.set_ylim(-20,30)
ax.set_xlim(1e3,3e7)
ax.yaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(2.5))
ax.scatter(freq, gain)
plt.tight_layout()
fig.savefig("mwe.pgf")
fig, ax = plt.subplots(figsize=(3.3914487339144874*0.5*1.5, 2.0960305886619515*0.8*1.5))
ax.grid(True,which='minor', alpha=0.3, axis='both')
ax.grid(True,which='major', alpha=0.7, axis='both')
ax.set_xscale('log')
ax.tick_params(axis='both', which='major', pad=2)
ax.set_ylim(-20,30)
ax.set_xlim(1e3,3e7)
ax.yaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(2.5))
ax.scatter(freq, gain)
plt.tight_layout()
fig.savefig("mwe2.pgf")
一份 MWE 乳胶文件:
\documentclass{article}
\usepackage{siunitx}
\usepackage{amsmath}
\usepackage{pgf}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\centering
\input{../mwe.pgf}
\qquad
\input{../mwe2.pgf}
\caption{How to force the same X axis ticklabels from the right hand plot to the left hand plot}
\label{fig:label}
\end{figure}
\end{document}