2

I have a scatter plot as the one below and would like my plots to have a double edge, without having to create the same scatter with the same coordinates on top of this one. I could not find how to have a double line as an edge.

import numpy as np
import matplotlib.pyplot as plt


N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)


plt.scatter(x, y, s=400, c=colors,marker='h' alpha=0.5,edgecolors='black',linewidth=1)
plt.show()

The main reason for this question comes from a bug I have: when I superimpose scatterplots with the same coordinates, the new plots I'm creating tends to modify slightly their positions and does not perfectly fits one over each otherbad fit

This bug does not show when the background marker has facecolors='' but only when it has facecolors='w' which is a problem for me.

MattnDo
  • 444
  • 1
  • 5
  • 17
  • could you show the exact code that gives the problem? and have you updated to matplotlib 2.0? – f5r5e5d Feb 26 '17 at 18:43
  • @f5r5ed Yep, updated. With the below code if I remove the N=50 and do the following, the bug is back: `cmap = plt.get_cmap('jet_r') colors = cmap(np.linspace(0.1, 0.9, cmap.N // 2)) whites = [[1,1,1]]` – MattnDo Feb 26 '17 at 19:43

1 Answers1

1

This seems to be indeed a bug.

A possible solution can be to use the colors argument to plot white scatter points.

import numpy as np
import matplotlib.pyplot as plt

N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
whites = [[1,1,1]]*N

plt.scatter(x, y, s=400, c=whites, marker='h', alpha=0.5,edgecolors='black',linewidth=1)
plt.scatter(x, y, s=260, c=colors, marker='h', alpha=0.5,edgecolors='black',linewidth=1)

plt.show()

enter image description here


Depending on the application using special symbols as markers may also be an option. See this question or the complete list.
import matplotlib.pyplot as plt

N = 4
x = [1,1,2,2]
y = [1,2,1,2]
symbols = [ur"$\u27C1$", ur"$\u25C8$", ur"$\u229A$", ur"$\u29C8$"]

for i in range(N):
    plt.scatter(x[i], y[i], s=400, c=(i/float(N), 0, 1-i/float(N)), marker=symbols[i], alpha=0.5,linewidth=1)
plt.show()

enter image description here

Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I don't know why but this seems to work! Thanks a lot. Just a last one, is there any way to "group" the newly created marker to avoid wrong overlapping ? – MattnDo Feb 26 '17 at 19:26
  • No, matplotlib cannot group markers. You would need to calculate yourself which markers are overlapping, as this mixes markersize in units of points with data units. (e.g. would points still overlap if you zoom in?). This can of course be automated but requires some lines of code. Using histogramming for that (`np.histogram2d`) might help. Or you could directly plot a histogram in matplotlib (`plt.hist2d` or `plt.hexbin`), but it would give some kind of different plot. – ImportanceOfBeingErnest Feb 27 '17 at 07:44
  • I have date and time in my dataframe so I might be able to combine this with `zorder` to make it work. Nice time ahead!! – MattnDo Feb 27 '17 at 08:47