Matplotlib: How to adjust linewidth in colorbar for contour plot?

前端 未结 2 1740
深忆病人
深忆病人 2021-01-12 14:42

Here\'s a minimal example to generate a plot which illustrates my question:

import matplotlib.pylab as plt
import matplotlib.mpl as mpl
import numpy as np
         


        
相关标签:
2条回答
  • 2021-01-12 15:12

    Use Axes.tick_params. As you use CB as the handle to the colorbar, the linewidth in colorbar can be set by:

    CB.ax.tick_params(size = your_size, width = your_width)
    

    size is the length of ticklines in the colorbar. width is the linewidth.

    0 讨论(0)
  • 2021-01-12 15:16

    You just need to find out how to access those lines, let try:

    >>> CB.ax.get_children()
    [<matplotlib.axis.XAxis object at 0x026A74B0>, <matplotlib.axis.YAxis object at 0x026AF270>, <matplotlib.lines.Line2D object at 0x026AF190>, <matplotlib.patches.Polygon object at 0x027387F0>, <matplotlib.collections.LineCollection object at 0x02748BD0>, <matplotlib.text.Text object at 0x026C0D10>, <matplotlib.patches.Rectangle object at 0x026C0D50>, <matplotlib.spines.Spine object at 0x026A7410>, <matplotlib.spines.Spine object at 0x026A7290>, <matplotlib.spines.Spine object at 0x026A7350>, <matplotlib.spines.Spine object at 0x026A71B0>]
    

    Alright, take a guess, I bet the 5th item is a list of the divider lines. We are looking for some .line objects and there are two. The first one (3rd item) actually is the edge of the entire color bar (if I remember correctly). So I will go for the next .line object.

    Now let's try to modified it in a few ways:

    >>> len(lines1[4].get_linewidths())
    11 #how many item are there? 11 lines
    >>> lines1[4].set_color(['r']*11) #set them all to red, in this example we actually want to have the color stay the same, this is just for a demonstration. 
    >>> lines1[4].set_linewidths([2]*11) #set them all to have linewidth of 2.
    

    the resultenter image description here

    0 讨论(0)
提交回复
热议问题