Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

like image 482
Sithis Avatar asked Aug 18 '15 13:08

Sithis


People also ask

How to change the color and add grid lines to matplotlib surface plot?

How to change the color and add grid lines to a Python Matplotlib surface plot? Set the figure size and adjust the padding between and around the subplots. Create x, y and h data points using numpy.

How to show/style the grid in Matplotlib?

Using the which argument, with possible values of major, minor or both, you can tell Matplotlib which grid you want to show or style. Matplotlib doesn't show the minor ticks / grid by default so you also need explicitly show them using minorticks_on (). Customizing the Grid and Minor Ticks

How do I plot a plot in Matplotlib?

First import the matplotlib library. Using the plt.plot method type the points or lines that you want to plot. In the brackets, if you don’t mention the line color as shown above; by default, it chooses the blue color.

How to show minor ticks/grid in Matplotlib?

Matplotlib doesn't show the minor ticks / grid by default so you also need explicitly show them using minorticks_on () . Now let's say you want to mimic the look of R's ggplot2 charts.


2 Answers

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

like image 185
Diziet Asahi Avatar answered Sep 19 '22 00:09

Diziet Asahi


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])
like image 35
raphael Avatar answered Sep 21 '22 00:09

raphael