How to remove marker edges in seaborn.pairplot()? - matplotlib

Seaborn has a handy function pairplot to create a matrix of scatter plots. Unfortunately, some standard matplotlib commands don't work with it.
sns.pairplot(matrix[cols[:4]].head(100), plot_kws=dict(alpha=.5, mew=0))
The markers get some ugly white edges. I tried mew for markeredgewidth keyword to remove them as it would be used in matplotlib, but that is an unknown property for seaborn. How can I remove these edges?

A scatter does not have a mew keyword. It is edgecolor instead. Hence
sns.pairplot(data, plot_kws=dict(edgecolor="none"))
would remove the white edge around the scatterpoints.

ImportanceOfBeingErnest's answer is much more precise. Alternatively, you can also use a workaround: Set the color of choice for both the face and the edges of the markers as (example from the docs)
import seaborn as sns
sns.set(style="ticks", color_codes=True)
iris = sns.load_dataset("iris")
g = sns.pairplot(iris, plot_kws=dict(facecolor='b', edgecolor="b"))
EDIT based on comments below IOBE's answer: Just specifying the linewidth=0 also works the same way on markers as specifying edgecolor="none".
g = sns.pairplot(iris, plot_kws=dict(linewidth=0))

Related

Changing Symbol/Marker Outline Width in Julia Using PyPlot

I'm trying to change the outline of the symbol (marker) inside a scatter plot in Julia using the PyPlot backend. I've tried edgecolor, edgewidth, edgelinewidth, markercolor, markerwidth, markerlinewidth and a variety of other key/values from various sources, but Julia/PyPlot recognizes none of these. How do I change the outline of the symbol/marker in a scatter plot executed in Julia using the PyPlot backend?
In Julia using the PyPlot backend, the correct key to manipulate the symbol/marker outline is markerstroke.... For example, markerstrokewidth=1 sets the outline of the marker to 1px, while markerstrokecolor="red" sets the outline color of the marker to red. I hope this helps.
For anyone reading this in 2022, PyPlot in Julia has completely changed. To change the edge size, the keyword is linewidths. In general, the repl is the best way to get up to date info. Just type ? (to get the help prompt), then PyPlot.scatter.

How do I use colourmaps with variable alpha in a Seaborn kdeplot without seeing the contour lines?

Python version: 3.6.4 (Anaconda on Windows)
Seaborn: 0.8.1
Matplotlib: 2.1.2
I'm trying to create a 2D Kernel Density plot using Seaborn but I want each step in the colourmap to have a different alpha value. I had a look at this question to create a matplotlib colourmap with alpha values: Add alpha to an existing matplotlib colormap.
I have a problem in that the lines between contours are visible. The result I get is here:
I thought that I had found the answer when I found this question: Hide contour linestroke on pyplot.contourf to get only fills. I tried the method outlined in the answer (using set_edgecolor("face") but it did not work in this case. That question also seemed to be related to vector graphics formats and I am just writing out a PNG.
Here is my script:
import numpy as np
import seaborn as sns
import matplotlib.colors as cols
import matplotlib.pyplot as plt
def alpha_cmap(cmap):
my_cmap = cmap(np.arange(cmap.N))
# Set a square root alpha.
x = np.linspace(0, 1, cmap.N)
my_cmap[:,-1] = x ** (0.5)
my_cmap = cols.ListedColormap(my_cmap)
return my_cmap
xs = np.random.uniform(size=100)
ys = np.random.uniform(size=100)
kplot = sns.kdeplot(data=xs, data2=ys,
cmap=alpha_cmap(plt.cm.viridis),
shade=True,
shade_lowest=False,
n_levels=30)
plt.savefig("example_plot.png")
Guided by some comments on this question I have tried some other methods that have been successful when this problem has come up. Based on this question (Matplotlib Contourf Plots Unwanted Outlines when Alpha < 1) I have tried altering the plot call to:
sns.kdeplot(data=xs, data2=ys,
cmap=alpha_cmap(plt.cm.viridis),
shade=True,
shade_lowest=False,
n_levels=30,
antialiased=True)
With antialiased=True the lines between contours are replaced by a narrow white line:
I have also tried an approach similar to this question - Pyplot pcolormesh confused when alpha not 1. This approach is based on looping over the PathCollections in kplot.collections and tuning the parameters of the edges so that they become invisible. I have tried adding this code and tweaking the linewidth -
for thing in kplot.collections:
thing.set_edgecolor("face")
thing.set_linewidth(0.01)
fig.canvas.draw()
This results in a mix of white and dark lines - .
I believe that I will not be able to tune the line width to make the lines disappear because of the variable width of the contour bands.
Using both methods (antialiasing + linewidth) makes this version, which looks cool but isn't quite what I want:
I also found this question - Changing Transparency of/Remove Contour Lines in Matplotlib
This one suggests overplotting a second plot with a different number of contour levels on the same axis, like:
kplot = sns.kdeplot(data=xs, data2=ys,
ax=ax,
cmap=alpha_cmap(plt.cm.viridis),
shade=True,
shade_lowest=False,
n_levels=30,
antialiased=True)
kplot = sns.kdeplot(data=xs, data2=ys,
ax=ax,
cmap=alpha_cmap(plt.cm.viridis),
shade=True,
shade_lowest=False,
n_levels=35,
antialiased=True)
This results in:
This is better, and almost works. The problem here is I need variable (and non-linear) alpha throughout the colourmap. The variable banding and lines seem to be a result of the combinations of alpha when contours are plotted over each other. I also still see some clear/white lines in the result.

Style of error bar in pandas plot

I'd like to plot line chart with error bar with the following style.
However, pandas plot draws error bars with only vertical line.
pd.DataFrame([1,2,3]).plot(yerr=[0.3,.3,.3])
How do I change style of error bar for pandas plot?
The versions are:
pandas '0.18.0'
matplotlib '1.5.1'
Update
One of the reason seems using the seaborn style. The following code give the nice style plot.
# plt.style.use('seaborn-paper')
pd.DataFrame([1,2,3]).plot(yerr=[0.3,.3,.3],capsize=4)
But, I have a reason to keep using seaborn style... Please help.
You can change the capsize inline when you call plot on your DataFrame, using the capsize kwarg (which gets passed on to plt.errorbar):
pd.DataFrame([1,2,3]).plot(yerr=[0.3,.3,.3],capsize=4)
Alternatively, you can change this setting using rcParams
You can find out what your default errorbar cap size is by printing plt.rcParams['errorbar.capsize']. If that is 0 (which is why I suspect you are currently getting no errorbar caps), you can set the default size of the errorbar caps to something nonzero, using:
plt.rcParams['errorbar.capsize']=4
Make sure to have that at the beginning of any plotting script.
Update:
It seems using the seaborn-paper style sets the cap thickness to 0. You can override this with the capthick kwarg:
plt.style.use('seaborn-paper')
pd.DataFrame([1,2,3]).plot(yerr=[0.3,.3,.3],capsize=4,capthick=1)

Change marker size in Seaborn Factorplot

I'm trying to change the markersize in Seaborn factorplots but I am not sure what keyword argument to pass
import seaborn as sns
exercise = sns.load_dataset("exercise")
g = sns.factorplot(x="time", y="pulse", hue="kind", data=exercise, ci= .95)
I tried passing markersize and s based off of these StackOverFlow answers but neither seem to have an effect
pyplot scatter plot marker size
Factorplot is calling the underlying function pointplot on default which accepts the argument markers. This is used to differentiate the markershapes. The size for all lines and markers can be changed with the scale argument.
exercise = sns.load_dataset("exercise")
g = sns.factorplot(x="time", y="pulse", hue="kind", data=exercise, ci=95,
markers=['o', 'v', 's'],
scale = 1.5)
Same data as above with different shapes
Please also note the ci argument in your example, .95 would result in a different figure with ci's hardly to see.

How to make a custom colormap using PyPlot (not matplotlib proper)

Working in IJulia. Desperately trying to make a custom colormap.
Tried the line:
matplotlib.colors.ListedColormap([(1,0,0),(0,1,0),(0,0,1)],"A")
which resulted in the following error
type PyObject has no field colors while loading In[16], in expression starting on line 1
which apparently means that I cannot use matplotlib directly, but only the functions which are in PyPlot.
I cannot involve matplotlib with an import (as this is invalid in IJulia).
I have noted that others have had help on similar problems, but that doesn't solve mine.
By using the PyCall package which PyPlot is using to wrap matplotlib you can obtain a colormap like this:
using PyCall
#pyimport matplotlib.colors as matcolors
cmap = matcolors.ListedColormap([(1,0,0),(0,1,0),(0,0,1)],"A")
In order to access fields in a PyObject you need to index the object with a symbol like:
cmap[:set_over]((0,0,0))
This is equivalent to: cmap.set_over((0,0,0)) in python. For other good examples of how to plot different kinds of plots using PyPlot, see these examples: https://gist.github.com/gizmaa/7214002
You don't need to use PyCall to call Python directly (although this is, of course, an option). You can also just use the PyPlot constructors for ColorMap to construct a colormap from (r,g,b) arrays or an array of colors as defined in the Julia Color package. See the PyPlot ColorMap documentation. For example:
using PyPlot, Color
ColorMap("A", [RGB(1,0,0),RGB(0,1,0),RGB(0,0,1)])