Matplotlib: Change color of individual grid lines
Asked Answered
D

2

8

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.

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. 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.

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? I've searched the internet for a solution for about 2 hours now and didn't really find anything helpful.

I thought something like ax1.get_xgridlines() might be used, but I have no idea how I could transform it into a useful list.

Thanks, Tamara

Deferent answered 18/8, 2015 at 13:18 Comment(0)
A
18

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

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)

enter image description here

Adlay answered 18/8, 2015 at 17:2 Comment(3)
Awesome, thank you, this is exactly what I needed! :)Deferent
any idea how this would work in case i've got major and minor gridlines and I want to adress the minor ones? (get_xgridlines() seems to return just the major grid)Emelia
What if I wanted to colour only a segment of the grid line (say 0.3 to 0.5 in the above figure)?Postulate
E
4

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):

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])
Emelia answered 3/10, 2018 at 9:15 Comment(1)
I came across the same problem and decided to switch off gridlines entirely & use custom vertical lines with axis.axvline(). But your pointer to the source code reason behind this issue is very helpful for those of us learning matplotlib...Extract

© 2022 - 2024 — McMap. All rights reserved.