The seaborn styles shipped by Matplotlib are deprecated since 3.6 - python

The seaborn styles shipped by Matplotlib are deprecated since 3.6,
as they no longer correspond to the styles shipped by seaborn.
However, they will remain available as 'seaborn-v0_8-<style>'. Alternatively, directly use the seaborn API instead.
I have tried this:
# use seaborn style
plt.style.use("seaborn")
but it is deprecated, and I want to remove this warning when I use the cmd in windows

This warning is telling you that seaborn styles in matplotlib do not match current seaborn styles, since the latest have been updated.
This is why you should set the style as follow:
plt.style.use("seaborn-v0_8")
You can specify a theme by replacing <style> with one the following:
white
dark
whitegrid
darkgrid
ticks
Just like this:
plt.style.use("seaborn-v0_8-whitegrid")
Alternatively, if you want to use the latest seaborn styles, use their library directly

I can confirm that adding the version to the use function, just as Tranbi described, works and removes the warning.
From the additional themes the one called "darkgrid" is almost the same as if I do not specify any.

Related

Matplotlib figure size in Jupyter reset by inlining in Jupyter

This question is more of a curiosity.
To change the default fig size to a custom one in matplotlib, one does
from matplotlib import rcParams
from matplotlib import pyplot as plt
rcParams['figure.figsize'] = 15, 9
after that, figure appears with chosen size.
Now, I'm finding something new (never happened/noticed before just now): in a Jupyter notebook, when inlining matplotlib as
%matplotlib inline
this apparently overwrites the rcParams dictionary restoring the default value for the figure size. Hence in oder to be able to set the size, I have to inline matplotlib before changing the values of the rcParams dictionary.
I am on a Mac OS 10.11.6, matplotlib version 1.5.1, Python 2.7.10, Jupyter 4.1.
IPython's inline backend sets some rcParams when it is initialized. This is configurable, and you can override it with your own configuration:
# in ~/.ipython/ipython_config.py
c.InlineBackend.rc = {
'figure.figsize': (15, 9)
}
The above would replace all of the rcParams that the inline backend sets, and you get total control. If you already have a matplotlib style that works nicely for inline output, you can tell the backend to leave everything alone:
c.InlineBackend.rc = {}
If you want to change just a few values, rather than overriding the whole thing, you can use the dictionary .update method:
c.InlineBackend.rc.update({'figure.figsize': (15, 9)})
In the future, the inline backend should be doing its defaults via matplotlib's nice new style mechanism, which should make it behave nicer in terms of respecting your preferences and allowing easier customization.

Transparent legend in Bokeh

In Matplotlib the legend function has a keyword argument called fancybox that makes the legend slightly transparent to see the lines behind the legend. Here is an example function call:
import matplotlib.pyplot as plt
plt.legend(fontsize='xx-small', loc='best', fancybox=True)
I can't find anything similar to this in Bokeh. Does anyone know if Bokeh has the functionality to make a plot transparent without going behind the scenes and monkey patching something in for it? Thanks.
I'm using bokeh 1.0.2 and the following worked for me:
# Make legend fully transparent
plot.legend.background_fill_alpha = 0.0
Judging by this one example in the documentation, I think you can change the legend transparency setting the plot.legend.border_line_alpha, which changes the transparency of the border line and also the legend itself, apparently.
http://docs.bokeh.org/en/latest/docs/user_guide/styling.html#id2
(As of Bokeh 0.9.2) Configuring the legend background was only just added a few weeks ago in a recent PR. It will be in the upcoming 0.9.3 release at the end of August 2015.
A final note, Just FYO: due to the cross-language nature of Bokeh, the capability to style the background is a feature and function of the client JS library, BokehJS. There's currently no amount of monkey patching from the python side that have any effect on making something that is not possible in BokehJS be possible. We are working on making BokehJS extensible from python, however.

How to set default matplotlib style?

When using matplotlib, I tend to use
import matplotlib.pyplot as plt
plt.style.use('ggplot')
quiet often. Is there an easy way to change the default style to ggplot (or any other style)? I looked into the 'matplotlibrc'-documentation but was unable to find an appropriate option.
Is there a better way then copying/linking the system-wide .mplstyle?
Thanks!
You can change the settings file of matplotlib. According to the docs :
matplotlib uses matplotlibrc configuration files to customize all kinds of properties, which we call rc settings or rc parameters. You can control the defaults of almost every property in matplotlib: figure size and dpi, line width, color and style, axes, axis and grid properties, text and font properties and so on.
You can locate your matplotlibrc file with the following command:
import matplotlib
matplotlib.matplotlib_fname()
Hence we can put ggplot settings at the end of the matplotlibrc file. You can easily find ggplot style (as well as other styles) on official matplotlib repository.
Apparently, there is no such option (yet).
However, you can tell iPython to load the ggplot style at startup, by adding "plt.style.use('ggplot')" to c.InteractiveShellApp.exec_lines in ipython_config.py.
Create a file (call it e.g. startup-01.py) in
~/.ipython/profile_default/startup/
(substitute another profile name for profile_default as needed)
and put there any needed notebook (and interactive ipython) initialization statements, including
import matplotlib.pyplot as plt
plt.style.use('ggplot')
## other settings, e.g.
# plt.rcParams['figure.figsize'] = (10.0, 8.0)
plt.style.use('default') worked for me.
As I understand this, it tells matplotlib to switch back to its default style mode.

ggplot styles in Python

When I look at the plotting style in the Pandas documentation, the plots look different from the default one. It seems to mimic the ggplot "look and feel".
Same thing with the seaborn's package.
How can I load that style? (even if I am not using a notebook?)
Update: If you have matplotlib >= 1.4, there is a new style module which has a ggplot style by default. To activate this, use:
from matplotlib import pyplot as plt
plt.style.use('ggplot')
To see all the available styles, you can check plt.style.available.
Similarly, for seaborn styling you can do:
plt.style.use('seaborn-white')
or, you can use seaborn's own machinery to set up the styling:
import seaborn as sns
sns.set()
The set() function has more options to select a specific style (see docs). Note that seaborn previously did the above automatically on import, but with the latest versions (>= 0.8) this is no longer the case.
If you actually want a ggplot-like syntax in Python as well (and not only the styling), take a look at the plotnine package, which is a grammar of graphics implementation in Python with a syntax very similar to R's ggplot2.
Note: the old answer mentioned to do pd.options.display.mpl_style = 'default'
. This was however deprecated in pandas in favor of matplotlib's styling using plt.style(..), and in the meantime this functionality is even removed from pandas.
For the themes in python-ggplot, you can use them with other plots:
from ggplot import theme_gray
theme = theme_gray()
with mpl.rc_context():
mpl.rcParams.update(theme.get_rcParams())
# plotting commands here
for ax in plt.gcf().axes:
theme.post_plot_callback(ax)
If you need to see available styles :
import matplotlib.pyplot as plt
print(plt.style.available)
This will print available styles.
And use this link to select the style you prefer
https://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html
Jan Katins's answer is good, but the python-ggplot project seems to have become inactive. The plotnine project is more developed and supports an analogous, but superficially different, solution:
from plotnine import theme_bw
import matplotlib as mpl
theme = theme_bw()
with mpl.rc_context():
mpl.rcParams.update(theme.rcParams)
While I think that joris answer is a better solution since you're using Pandas, it should be mentioned that Matplotlib can be set to mimic ggplot by issuing the command matplotlib.style.use('ggplot').
See examples in the Matplotlib gallery.

Matplotlib turn off antialias for text in plot?

Is there any way to turn off antialias for all text in a plot, especially the ticklabels?
It seems this is not possible. Some classes such as Line2D have a "set_antialiased" method, but Text lacks this. I suggest you file a feature request on the Sourceforge tracker, and send an email to the matplotlib mailing list mentioning the request.
Did you try playing with the font/text properties of matplotlibrc ? That would be the first thing to try, I think (see here for examples: http://matplotlib.sourceforge.net/users/customizing.html)
I believe the anti-aliasing of Text objects is up to the font engine being used. It seems the freetype2 fonts support this.
Not sure if it already existed back in 2010, but I had the same issue and found that matplotlib has a text.antialiased parameter that applies to the tick labels too. Tested with the agg and cairo backends:
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['text.antialiased'] = False
plt.plot([0,1,2], [3,4,5])

Categories