Matplotlib turn off antialias for text in plot? - python

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])

Related

Matplotlib background matches vscode theme on dark mode and can't see axis

I just got a new PC and downloaded visual studio code. I'm trying to run the exact same plots as the code I had on my other PC (just plt.plot(losses)) but now matplotlib seems to have a dark background instead of white:
I found this and this that had opposite problems.
To clarify, I'm asking how to change the matplotlib background plots to white (note that in my other machine I didn't have to hard code any matplotlib background information so I think it's a visual studio problem, but couldn't figure it out)
Difficult to be sure since I cannot reproduce your problem.
Two things to try (both presume that you import matplotlib using import matplotlib.pyplot as plt):
if you use plt.figure, add facecolor='white' parameter. Or try to run fig.set_facecolor('white') (fig here is the variable that stored the figure which facecolor you are changing. If you don't have any, use plt.gcf().set_facecolor('white') once the figure is created; gcf() returns current figure, see this doc).
Try to change plt.style.context as in this matplotlib example.
You could also set the runtime configuration (rcParams) to change it for the entire sacript/notebook (documentation)
import matplotlib as mpl
mpl.rcParams['figure.facecolor'] = 'white'
Changing this setting did the trick for me
in the settings-extensions-python-experiments: Opt Out Form-
there is and option that says:
jupyter.themeMatplotlibPlots": false,
if true mark it false this worked for me

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.

Multiple figure arrangement using Matplotlib

Can we control where Matplotlib places figures on the screen?
I want to generate four figures (in four separate windows) that do not overlap.
From IPython you can do the following:
figure()
get_current_fig_manager().window.wm_geometry("400x600+20+40")
Or equivalently in a Python script:
import pylab as pl
pl.figure()
pl.get_current_fig_manager().window.wm_geometry("400x600+20+40")
pl.show()
Note that this assumes you're using the TkAgg backend.
It is also possible to use the IPython interface with the Qt backend to achieve a similar result:
import matplotlib
import pylab as pl
f1 = pl.figure()
f_manager = pl.get_current_fig_manager()
f_manager.window.move(600, 600)
pl.show()
With f_manager you basically have a PyQt4 object that allows you to modify the window properties as you like.
Not using show() and Matplotlib alone. The simplest solution may be to use savefig(..) and use your favorite OS image viewer. If you need interactivity with the plots, Matplotlib offers backends.
The easiest way I know to do this is to make the window for the figure in your preferred GUI application, and then put the matplotlib figure into this window. There are a bunch of examples of how to do this embedding using different GUI frameworks here.
The code samples can look a bit complicated, but it's mostly boilerplate where you'll only need to modify a few lines.

Categories