简体   繁体   English

Matplotlib:更改单个网格线的颜色

[英]Matplotlib: Change color of individual grid lines

I've only been using Python for about a month now, so I'm sorry if there's some simple solution to this that I overlooked. 我现在只使用Python大约一个月,所以我很抱歉,如果有一些简单的解决方案我忽略了。

Basically I have a figure with 4 subplots, the 2 on the left show longitudinal plots and the ones on the right show scatter plots at certain points of the longitudinal plots. 基本上我有一个有4个子图的图,左边的2显示纵向图,右边的图显示纵向图的某些点的散点图。 You can click through the scatter plots at different points of the longitudinal plot with buttons, and the tick label of the longitudinal plot you're currently at will be highlighted in blue. 您可以使用按钮单击纵向图的不同点处的散点图,并且您当前所在纵向图的刻度标签将以蓝色突出显示。

Coloring a certain tick label already works with this: 着色某个刻度标签已经适用于此:

xlabels = []
labelcolors = []

for i, item in enumerate(mr.segmentlst):
    if re.search('SFX|MC|MQ|MS|MKC', item):
        xlabels.append(mr.segmentlst[i])
    else:
        xlabels.append('')

for i, item in enumerate(mr.segmentlst):
    if re.search('SFX', item):
        labelcolors.append('black')
    else:
        labelcolors.append('gray')

labelcolors[self.ind]='blue'      

[t.set_color(i) for (i,t) in zip(labelcolors, ax1.xaxis.get_ticklabels())]
[t.set_color(i) for (i,t) in zip(labelcolors, ax2.xaxis.get_ticklabels())]

It only shows certain tick labels and changes their colors accordingly (I don't know if there is another solution for this, it's the only one I could find). 它只显示某些刻度标签并相应地改变它们的颜色(我不知道是否有另一个解决方案,它是我能找到的唯一一个)。 Don't mind the mr.segmentlist, I've currently hardcoded the plot to use an attribute from another method so I can easily keep testing it in Spyder. 不介意mr.segmentlist,我目前已经硬编码使用另一种方法的属性,所以我可以轻松地在Spyder中测试它。

I'd like to also change the grid line color of the currently highlighted tick label (only xgridlines are visible) in the longitudinal plots, is there some kind of similar way of doing this? 我还想在纵向图中更改当前突出显示的刻度标签的网格线颜色(只有xgridlines可见),是否有某种类似的方法可以做到这一点? I've searched the internet for a solution for about 2 hours now and didn't really find anything helpful. 我现在在互联网上搜索了大约2个小时的解决方案,并没有找到任何有用的东西。

I thought something like ax1.get_xgridlines() might be used, but I have no idea how I could transform it into a useful list. 我认为可能会使用像ax1.get_xgridlines()这样的东西,但我不知道如何将其转换为有用的列表。

Thanks, Tamara 谢谢,塔玛拉

get_xgridlines() returns a list of Line2D objects, so if you can locate which line you want to modify, you can modify any of their properties get_xgridlines()返回Line2D对象的列表,因此如果您可以找到要修改的行,则可以修改其任何属性

x = np.random.random_sample((10,))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x,x)
ax.grid()
a = ax.get_xgridlines()
b = a[2]
b.set_color('red')
b.set_linewidth(3)

在此输入图像描述

since the above solution only works with major gridlines (since get_gridlines() is currently hardcoded to use only the major ones), here's how you can also access the minor gridlines by adapting the get_gridlines() function (from here) : 因为上面的解决方案只适用于主要的网格线(因为get_gridlines()当前是硬编码的,只使用主要的网格线),这里你也可以通过调整get_gridlines()函数(从这里)来访问网格线:

from matplotlib import cbook
def get_gridlines(ax, which):
        '''
        Parameters: 
            ax : ax.xaxis or ax.yaxis instance

            which : 'major' or 'minor'
        Returns:
            The grid lines as a list of Line2D instance    

        '''

        if which == 'major':
            ticks = ax.get_major_ticks()
        if which == 'minor':
            ticks = ax.get_minor_ticks()

        return cbook.silent_list('Line2D gridline',
                                 [tick.gridline for tick in ticks])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM