0
我想刪除對應於自定義xtick的垂直網格線(在下圖中顯示在x = 71處)。我可以通過使用hack刪除下圖中對應於ytick 701的水平網格線:由於我在y軸上沒有小勾號,因此我定義了與指向最大值的線相對應的自定義ytick,並穿過y軸作爲次要記號,然後禁用y軸上的次要記號的網格線。不幸的是,我不能在x軸上使用相同的黑客而不禁用小勾號的網格線,這是我不惜一切代價避免的。如何去除對應於對數刻度軸上自定義xtick的特定網格線?
下面是一個不那麼簡單但仍然是我們。
有很多事情我不明白,2個專業的學生爲什麼
locs, labels = plt.xticks()
不返回繪製和LOCS和標籤爲什麼我沒有得到顯示爲10 xticks標籤^ X其中x = 0,1,2和3,但這超出了原始問題的範圍。
import matplotlib.pyplot as plt
plt.grid(True)
import numpy as np
# Generate data
x_data = np.arange(1, 1000 , 10)
y_data = np.random.lognormal(1e-5, 3, len(x_data))
y_max = max(y_data)
# plot
plt.xscale('log')
import math
ratio_log = math.log(x_data[np.argmax(y_data)])/math.log(max(x_data)) # I need to do this in order to plot a horizontal red dashed line that points to the max and do not extend any further.
plt.axhline(y=y_max, xmin=0, xmax = ratio_log, color='r', linestyle='--') # horizontal line pointing to the max y value.
axes = plt.gca()
axes.set_xlim([1, max(x_data)]) # Limits for the x axis.
# custom ticks and labels
# First yticks because I'm able to achieve what I seek
axes.set_yticks([int(y_max)], minor=True) # Sets the custom ytick as a minor one.
from matplotlib.ticker import FormatStrFormatter
axes.yaxis.set_minor_formatter(FormatStrFormatter("%.0f"))
axes.yaxis.grid(False, which='minor') # Removes minor yticks grid. Since I only have my custom yticks as a minor one, this will disable only the grid line corresponding to that ytick. That's a hack.
import matplotlib.ticker as plticker
loc = plticker.MultipleLocator(base=y_max/3.3) # this locator puts ticks at regular intervals. I ensure the y axis ticks look ok.
axes.yaxis.set_major_locator(loc)
# Now xticks. I'm having a lot of difficulty here, unable to remove the grid of a particular custom xticks.
locs, labels = plt.xticks() # Strangely, this doesn't return the locs and labels that are plotted. There are indeed 2 values that aren't displayed in the plot, here 1.00000000e-01 and 1.00000000e+04. I've got to remove them before I can append my custom loc and label.
# This means that if I do: plt.xticks(locs, labels) right here, it would enlarge both the lower and upper limits on the x axis... I fail to see how that's intuitive or useful at all. Might this be a bug?
locs = np.append(locs[1:-1], np.asarray(x_data[np.argmax(y_data)])) # One of the ugliest hack I have ever seen... to get correct ticks and labels.
labels = (str(int(loc)) for loc in locs) # Just visuals to get integers on the axis.
plt.xticks(locs, labels) # updates the xticks and labels.
plt.plot((x_data[np.argmax(y_data)], x_data[np.argmax(y_data)]), (0, y_max), 'r--') # vertical line that points to the max. Non OO way to do it, so a bad way.
plt.plot(x_data, y_data)
plt.savefig('grid_prob.png')
plt.close()
例圖片下面(的代碼每次執行時輸出一個不同的畫面,但問題出現在所有圖片)。
該問題可能是一個有效的問題,但我有問題了解問題。就像我看到的那樣,你手動在位置71添加網格;那麼你問如何刪除它。根本不顯示網格會更容易,而是在需要的地方添加一條簡單的線條? – ImportanceOfBeingErnest
我沒有看到我手動在位置71添加網格的位置。我看到我用plt.grid(True)啓用了網格,這就是我所做的與x軸網格有關的所有事情。當然,我添加了xticks,但手動沒有網格,除非我錯過了一些東西。回答你的問題,我不知道。然而,你的評論幫助我解決了這個問題(儘管我沒有做到這一點)。看到我發佈的答案。另外,如果你有更高效的方法來實現相同的結果,例如不用plt.grid(True)加載網格,或者甚至使用.set_visible(),我都會很樂意接受你的答案 –
。我也意識到,我只添加了代碼行,使代碼更復雜,所以我可能沒有按照您提到的確切方式解決問題。所以請隨時繼續併發布您的解決方案。 –