簡體   English   中英

matplotlib 在小 plot 的對數軸上施加更密集的主要刻度

[英]matplotlib force denser major ticks on logaritmic axis of a small plot

我有一個帶有對數 X 軸的 matplotlib plot,我想將其保存為 pgf 並將其包含到我的 latex 文檔中。 在所需的尺寸 matplotlib 不 label 在 X 軸上的每輪底數 = 10。 另一方面,如果我增加 plot 的尺寸,它們會與短軸一起顯示。 我找到了 LogLocator,但我無法說服它生成我想要實現的 output。

matplotlib output和所需的output的區別: 在此處輸入圖像描述

我使用的代碼:

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 latex 文檔:

\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}

我只是解決方案的一次嘗試。 以下行將解決問題:

ax.xaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10, subs=[1], numticks=6))
ax.xaxis.set_minor_locator(matplotlib.ticker.LogLocator(base=10, subs=[1,2,3,4,5,6,7,8,9],numticks=9))  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM