52

In this example of a marker from my scatter plot I have set the color to green, and edge color to black, and hatch to "|". For the hatch pattern to show up at all I must set the edgecolor, however when I do, I get a very thick border around the marker. Two questions:

1) How can I to set the size of this border (preferably to 0)?

2) How can I increase the thickness of the hatch lines?

Seanny123
  • 7,611
  • 11
  • 61
  • 115
David E
  • 828
  • 2
  • 7
  • 13
  • To 2.: Take a look at [How to change the linewidth of hatch in matplotlib?](http://stackoverflow.com/questions/29549530/how-to-change-the-linewidth-of-hatch-in-matplotlib) – Henhuy Feb 26 '16 at 11:08

2 Answers2

75
  1. You just need to set the linewidth to control the marker border thickness.
  2. You can increase the density of hatching, by repeating symbols (in the example below, the '|' is repeated in the R/H pane; note that to obtain NW->SE diagonal lines the symbol must be escaped so needs twice as many characters to really double it -- '\\\\' is density 2 while '||||' is density 4). However, I don't think the thickness of individual lines within hatching is controllable.

See the code example below to produce scatter plots such as these: example hatching control

import matplotlib.pyplot as plt
# generate some data
x = [1,2,3,4,5,8]
y= [i**2 for i in x]
y2= [60-i**2+3*i for i in x]

# plot markers with thick borders
plt.subplot(121)
plt.scatter(x,y, s=500, marker='s', edgecolor='black', linewidth=3, facecolor='green', hatch='|')
# compare with no borders, and denser hatch.
plt.subplot(122)
plt.scatter(x,y2, s=500, marker='s', edgecolor='black', linewidth=0, facecolor='green', hatch='||||')

plt.show()

matplotlib documentation on collections and scatter.

Bonlenfum
  • 17,776
  • 2
  • 50
  • 52
  • You are correct about not being able to change the width of the hatch lines, it is hard coded to 1 (at least in the aggbackend) – tacaswell Jan 15 '13 at 05:14
  • and adding the ability to tune the size of the hatches looks like a huge undertaking..... – tacaswell Jan 15 '13 at 05:40
  • When dealing with multiples of the character `'\'`, it may be helpful to use the Python string multiplication operator. For example, due to character escaping, for a single `'\'` character, you would use the string `"\\"`. For `n` repetitions, you can use `n * "\\"`, e.g., `4 * "\\"` would give a string of four `'\'` characters. – naitsirhc Feb 14 '15 at 14:55
  • 1
    Surely the correct argument to control the marker border thickness is `markeredgewidth` (or `mew`)? – binaryfunt Mar 05 '16 at 16:48
  • 2
    It seems to be controlled by `markerlinewidth` now. – tommy.carstensen Sep 15 '17 at 06:27
  • `edgecolors='none'` works for me when calling `plot.scatter` on a pandas DataFrame (see https://stackoverflow.com/a/9082655/1878788) – bli Dec 08 '17 at 15:22
  • `markeredgewidth` and `mew` worked for me, but `markerlinewidth` and `linewidth` did not. – crypdick Jul 03 '18 at 18:01
  • The current version of matplotlib (3.2.1) doesn't allow linewidth to be a string. It needs to be a number. – ZYX Jun 14 '20 at 18:58
7

This is several years after you asked the question, but the only way I've found to do it is to change the matplotlib.rc.

You can do this either in the actual .rc file or within your python script, e.g.

import matplotlib as mpl

mpl.rc('hatch', color='k', linewidth=1.5)

This will make all of the hatch lines in your script black and thickness 1.5 rather than the default 1.0.

Jacob
  • 71
  • 1
  • 1