2017-08-31 65 views
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() 

例图片下面(的代码每次执行时输出一个不同的画面,但问题出现在所有图片)。

Output file illustrating the problem I have.

+1

该问题可能是一个有效的问题,但我有问题了解问题。就像我看到的那样,你手动在位置71添加网格;那么你问如何删除它。根本不显示网格会更容易,而是在需要的地方添加一条简单的线条? – ImportanceOfBeingErnest

+0

我没有看到我手动在位置71添加网格的位置。我看到我用plt.grid(True)启用了网格,这就是我所做的与x轴网格有关的所有事情。当然,我添加了xticks,但手动没有网格,除非我错过了一些东西。回答你的问题,我不知道。然而,你的评论帮助我解决了这个问题(尽管我没有做到这一点)。看到我发布的答案。另外,如果你有更高效的方法来实现相同的结果,例如不用plt.grid(True)加载网格,或者甚至使用.set_visible(),我都会很乐意接受你的答案 –

+0

。我也意识到,我只添加了代码行,使代码更复杂,所以我可能没有按照您提到的确切方式解决问题。所以请随时继续并发布您的解决方案。 –

回答

0

信用为理念去@ImportanceOfBeingErnest对谁我非常感谢。

我除去网格

axes.xaxis.grid(False, which='both') 

,然后我添加了一个网格对应于每个XTICK除了自定义一个与下面的循环:

for loc in locs[1:-1]: 
    if loc != x_data[np.argmax(y_data)]: 
     plt.axvline(x=loc, color = 'grey', linestyle = '-', linewidth = 0.4)                            

插入该代码只是行

之前
plt.xticks(locs, labels) # updates the xticks and labels. 

下面的输出图片示例。 no problem now.

相关问题