I am using Seaborn to plot some data in Pandas.
I am making some very large plots (factorplots).
To see them, I am using some visualisation facilities at my university.
I am using a Compound screen made up of 4 by 4 monitors with small (but nonzero) bevel -- the gap between the screens.
This gap is black.
To minimise the disconnect between the screen i want the graph backgound to be black.
I have been digging around the documentation and playing around and I can't work it out..
Surely this is simple.
I can get grey background using set_style('darkgrid')
do i need to access the plot in matplotlib directly?
seaborn.set takes an rc argument that accepts a dictionary of valid matplotlib rcparams. So we need to set two things: the axes.facecolor, which is the color of the area where the data are drawn, and the figure.facecolor, which is the everything a part of the figure outside of the axes object.
(edited with advice from #mwaskom)
So if you do:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn
seaborn.set(rc={'axes.facecolor':'cornflowerblue', 'figure.facecolor':'cornflowerblue'})
fig, ax = plt.subplots()
You get:
And that'll work with your FacetGrid as well.
I am not familiar with seaborn but the following appears to let you change
the background by setting the axes background. It can set any of the ax.set_*
elements.
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
m=pd.DataFrame({'x':['1','1','2','2','13','13'],
'y':np.random.randn(6)})
facet = sns.factorplot('x','y',data=m)
facet.set(axis_bgcolor='k')
plt.show()
Another way is to set the theme:
seaborn.set_theme(style='white')
In new versions of seaborn you can also use
axes_style() and set_style() to quickly set the plot style to one of the predefined styles: darkgrid, whitegrid, dark, white, ticks
st = axes_style("whitegrid")
set_style("ticks", {"xtick.major.size": 8, "ytick.major.size": 8})
More info in seaborn docs
Related
I need some help to make my cph plot bigger, but unfortunately, it seems like figsize can't be applied on this plot! Can somebody help me please?
I'm using Jupyter Notebook on pandas!
cph.plot()
Here the problem is that the plot function actually plots my features, but they are too much so their names overlap and I can see nothing! I need the plot to be bigger!
Seems like cph.plot() calls matplotlib.pyplot.plot in the back-end. By default, Matplotlib uses the last created figure, so creating a figure with your specified width and height should do the trick:
import matplotlib.pyplot as plt
# 8, 12 => width and height in inches
plt.figure(figsize=(8, 12))
cph.plot(/*your params here*/)
See if this works.
you can try the following command:
import seaborn as sns
sns.set(rc={'figure.figsize':(18,10)})
cph.plot()
I finished analyzing my data and want to show that they are statistically significant using the t-test_ind. However, I haven't found anything functional to show this other than what was referenced in (How does one insert statistical annotations (stars or p-values) into matplotlib / seaborn plots?):
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
from statannot import add_stat_annotation
ax = sns.barplot(x=x, y=y, order=order)
add_stat_annotation(ax, data=df, x=x, y=y,
boxPairList=[(order[0], order[1]), (order[0], order[2])],
test='t-test_ind',
textFormat='star',
loc='outside')
Using this approach however, whenever I try to save the plot using plt.savefig() the added significancies using the add_stat_annotation are discared (matplotlib does not seem to recognize the added annotations). Using the loc='inside' option messes up my plot so it isn't really an option.
I am therefore asking if there is some simpler way to add the sigificancies directly in matplotlib / seaborn or if you can plt.savefig() with enough border / padding to include everything.
It was mainly a xlabel cut off problem. So in future applications I would use the add_stat_annotation from webermarcolivier/statannot. To save your files use one of the following possibilities:
import matplotlib.pyplot as plt
plt.tight_layout() # Option 1
plt.autoscale() # Option 2
plt.savefig('filename.png', bbox_inches = "tight") # Option 3
Hope this will help someone for future use.
In regular matplotlib you can specify various marker styles for plots. However, if I import seaborn, '+' and 'x' styles stop working and cause the plots not to show - others marker types e.g. '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 this: Simple Seaborn Plot
Changing 'ok' on line 6 to '+k' however, no longer shows the plotted point. If I don't import seaborn it works as it should: Regular Plot With Cross Marker
Could someone please enlighten me as to how I change the marker style to a cross type when seaborn being used?
The reason for this behaviour is that seaborn sets the marker edge width to zero. (see source).
As is pointed out in the seaborn known issues
An unfortunate consequence of how the matplotlib marker styles work is that line-art markers (e.g. "+") or markers with facecolor set to "none" will be invisible when the default seaborn style is in effect. This can be changed by using a different markeredgewidth (aliased to mew) either in the function call or globally in the rcParams.
This issue is telling us about it as well as this one.
In this case, the solution is to set the markeredgewidth to something larger than zero,
using rcParams (after importing seaborn):
plt.rcParams["lines.markeredgewidth"] = 1
using the markeredgewidth or mew keyword argument
plt.plot(..., mew=1)
However, as #mwaskom points out in the comments, there is actually more to it. In this issue it is argued that markers should be divided into two classes, bulk style markers and line art markers. This has been partially accomplished in matplotlib version 2.0 where you can obtain a "plus" as marker, using marker="P" and this marker will be visible even with markeredgewidth=0.
plt.plot(x_cross, y_cross, 'kP')
It is very like to be a bug. However you can set marker edge line width by 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()
I am working on generating some scatter plot with matplotlib.pyplot.scatter() in jupyter notebook, and I found that if I import seaborn package, the scatter plot will lose its color. I am wondering if anyone has a similar issue?
Here is an example code
import matplotlib.pyplot as plt
import seaborn as sb
plt.scatter(range(4),range(4), c=range(4))
The output is
The scatter plot without seaborn is:
That seems to be the way it behaves. In seaborn 0.3 the default color scale was changed to greyscale. If you change your code to:
plt.scatter(range(4),range(4), c=sb.color_palette())
You will get an image with colors similar to your original.
See the Seaborn docs on choosing color palettes for more info.
Another way to fix this is to specify cmap option for plt.scatter() so that it would not be affected by seaborn:
ax = plt.scatter(range(4),range(4), c=range(4), cmap='gist_rainbow')
plt.colorbar(ax)
The result is:
There are many options for cmap here:
http://matplotlib.org/examples/color/colormaps_reference.html
seaborn is a beautiful Python package that acts, for the most part, as an additional layer on top of matplotlib. However, it changes, for instance, things that would be matplotlib methods on a plot object to direct seaborn functions.
seaborn's despine() remove any spines (the outer edges of the plot) from a plot. But I cannot do the opposite.
I cannot seem to recreate the spine in the standard way that I would / could if I had used matplotlib entirely from the start. Is there a way to do that? How would I?
Below is an example. Could I, for instance, add a spine on the bottom and the left of the plot?
from sklearn import datasets
import pandas as pd
tmp = datasets.load_iris()
iris = pd.DataFrame(tmp.data, columns=tmp.feature_names)
iris['species'] = tmp.target_names[tmp.target]
iris.species = iris.species.astype('category')
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('darkgrid')
sns.boxplot(x='species', y='sepal length (cm)', data=iris_new)
plt.show()
Thanks for all the great comments! I knew some of what you wrote, but not that both the 'axes.linewidth' and 'axes.edgecolor' needed to be set.
I'm writing an answer here, since it is a compilation of a few comments.
That is, the following code generates the plot below:
sns.set_style('darkgrid', {'axes.linewidth': 2, 'axes.edgecolor':'black'})
sns.boxplot(x='species', y='sepal length (cm)', data=iris_new)
plt.show()