2015-06-17 187 views
3

有誰知道如何使用Python/Matplotlib以對數刻度顯示次要刻度的標籤嗎?如何使用Matplotlib以對數刻度顯示次要刻度標籤

謝謝!

+0

你有沒有看函數[set_tick_params()](http://matplotlib.org/api/axis_api.html)?該文檔說:_設置ticks和ticklabels的外觀參數._ –

+0

此問題似乎是http://stackoverflow.com/questions/17165435/matplotlib-show-labels-for-minor-ticks-also/17167748# 17167748 –

+0

如果有人正在尋找一個解決方案來顯示超過10多年的日誌軸上的次要刻度,下面的解決方案將無法工作,並且可能會看到[此問題](http:// stackoverflow。 com/questions/44078409/matplolib-semi-log-plot-minor-tick-marks-are-gone-when-range-is-large)。 – ImportanceOfBeingErnest

回答

10

您可以使用plt.tick_params(axis='y', which='minor')設置小勾號並使用matplotlib.tickerFormatStrFormatter對其進行格式設置。例如,

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.ticker import FormatStrFormatter 
x = np.linspace(0,4,1000) 
y = np.exp(x) 
plt.plot(x, y) 
ax = plt.gca() 
ax.set_yscale('log') 
plt.tick_params(axis='y', which='minor') 
ax.yaxis.set_minor_formatter(FormatStrFormatter("%.1f")) 
plt.show() 

enter image description here

+0

我認爲OP在問他是否需要一些小勾號上的標籤。就像在這個相關的答案:http://stackoverflow.com/a/17167748/4716013 –

+0

@prodev_paris - 你是對的:我編輯我的答案,使其更完整。 – xnx

+0

非常感謝@xnx,這真的很好! – Tomas