Python: changing marker type in Seaborn
In regular matplotlib, you can specify different marker styles for plots. However, if I import styles seaborn
, the "+" and "x" styles stop working and cause the graphs to fail to display - other types of markers, for example. 'o', 'v' and '*' work.
Simple example:
import matplotlib.pyplot as plt
import seaborn as sns
x_cross = [768]
y_cross = [1.028e8]
plt.plot(x_cross, y_cross, 'ok')
plt.gca().set_xlim([10, 1e4])
plt.gca().set_ylim([1, 1e18])
plt.xscale('log')
plt.yscale('log')
plt.show()
Produces the following: Simple Seaborn Site
Changing "ok" on line 6 to "+ k", however, no longer displays the typed period. If I don't import seaborn
it works as it should: Regular cross-marker plot
Can anyone please tell me how to change the marker style to cross-type when used seaborn
?
source to share
The reason for this behavior is that the sea wave sets the marker edge width to zero. (see source ).
As noted in known issues at sea
An unfortunate consequence of the way matplotlib marker styles work is that line markers (for example
"+"
) or markersfacecolor
set to"none"
will be invisible when the default seabed style is in effect. This can be changed by using anothermarkeredgewidth
(aliasmew
) either in the function call or globally in rcParams.
This question tells us about it as well as this one .
In this case, the solution is to set the marked word width to something greater than zero,
-
using rcParams (after seabed import):
plt.rcParams["lines.markeredgewidth"] = 1
-
using a keyword argument
markeredgewidth
ormew
plt.plot(..., mew=1)
However, as @mwaskom points out in the comments, there is actually more of it. In this issue it argued that the markers should be divided into two classes, markers and markers surround style line art. This was partially done in matplotlib version 2.0, where you can get a plus as a marker using marker="P"
, and this marker will be visible even with markeredgewidth=0
.
plt.plot(x_cross, y_cross, 'kP')
source to share
This looks a lot like a bug. However, you can set the line width of the marker edge to a mew
keyword to get what you want:
import matplotlib.pyplot as plt
import seaborn as sns
x_cross = [768]
y_cross = [1.028e8]
# set marker edge line width to 0.5
plt.plot(x_cross, y_cross, '+k', mew=.5)
plt.gca().set_xlim([10, 1e4])
plt.gca().set_ylim([1, 1e18])
plt.xscale('log')
plt.yscale('log')
plt.show()
source to share