Use a different matplotlibrc for savefig - matplotlib

I am using Jupyter Notebook, with a matplotlibrc style that's consistent with its theme set using jupyterthemes. That plotting style however does not look good if I want to export it to PNG to use it within my other documents.
How do I specify a different matplotlibrc when I do a savefig?

Most matplotlib style settings are applied at the moment the object they apply to is created.
You would hence need to create two different plots, one with the usual style of your notebook and another one with the style from the style file. The latter one would be the one to save.
A decent solution would be to create a plot in a function. You can then call this function within a context, with plt.style.context(<your style>): to give the figure a different style.
import matplotlib.pyplot as plt
def plot():
fig, ax = plt.subplots()
ax.plot([2,3,4], label="label")
ax.legend()
# Plot with general style of the notebook
plot()
# Plot with your chosen style for saved figures
with plt.style.context('ggplot'):
plot()
plt.savefig("dark.png")
#plt.close(plt.gcf()) # if you don't want to show this figure on screen
plt.show()
Relevant here: The matplotlib customizing guide.

Perusing matplotlib/__init__.py reveals a number of functions used for managing rcParams. To update rcParams from a file, use matplotlib.rc_file:
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc_file('/tmp/matplotlibrc')
plt.plot([0,1], [0,10])
plt.savefig('/tmp/out.png')
with /tmp/matplotlibrc containing
lines.linewidth : 10 # line width in points
lines.linestyle : -- # dashed line
yields
PS. In hindsight, having found rc_file, googling shows it is documented here.

Related

Draw an ordinary plot with the same style as in plt.hist(histtype='step')

The method plt.hist() in pyplot has a way to create a 'step-like' plot style when calling
plt.hist(data, histtype='step')
but the 'ordinary' methods that plot raw data without processing (plt.plot(), plt.scatter(), etc.) apparently do not have style options to obtain the same result. My goal is to plot a given set of points using that style, without making histogram of these points.
Is that achievable with standard library methods for plotting a given 2-D set of points?
I also think that there is at least one hack (generating a fake distribution which would have histogram equal to our data) and a 'low-level' solution to draw each segment manually, but none of these ways seems favorable.
Maybe you are looking for drawstyle="steps".
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
data = np.cumsum(np.random.randn(10))
plt.plot(data, drawstyle="steps")
plt.show()
Note that this is slightly different from histograms, because the lines do not go to zero at the ends.

Determining matplotlib stylesheet keys

Is there a standard method for determining valid stylesheet keys for matplotlib? Currently I use a mixture of searching on SO, guesswork and this example stylesheet as a reference. Is there a complete list of valid keys for a stylesheets?
For example if I want to set the colour on a scatter graph to black I could use:
import matplotlib as plt
fig, ax = plt.subplots()
ax.scatter(x, y, color='k')
However axes.scatter.color is not a valid key for a stylesheet. I can set the marker type using the key scatter.marker, but scatter.marker.color doesn't exist.
All valid rc Params are part of the matplotlib rc file shown on the matplotlib page. (If you find a valid rc param that is missing in that file, please report it to the matplotlib GitHub tracker.)
You may also print a list of all rc Params as
import matplotlib.pyplot as plt
for k,v in plt.rcParams.items():
print(k)

Output figure size in Jupyter Notebook

I just upgraded my Jupyter version to 4.2.1 and it seems as though inline figures have gotten a lot larger for the same figsize.
Am I imagining this?
Can I change that without changing the figsize?
You can use top-level matplotlib settings like this:
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (10,10)
This will change default figure size to 10x10. More on that in the documentation: http://matplotlib.org/users/customizing.html
The scaling of the figure for figures with %matplotlib inline depends on the figure.dpi parameter. So if your figures are to small and you do not want to increase the figsize then you can just set
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 150 # default for me was 75
I also had the impression that the scaling changed at some point, but I guess this can also appear if you change your screen/resolution.

X-axis labels on Seaborn Plots in Bokeh

I'm attempting to follow the violin plot example in bokeh, but am unable to add x-axis labels to my violins. According to the Seaborn documentation it looks like I should be able to add x-axis labels via the "names" argument, however, the following code does not add x-axis labels:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from bokeh import mpl
from bokeh.plotting import show
# generate some random data
data = 1 + np.random.randn(20, 6)
# Use Seaborn and Matplotlib normally
sns.violinplot(data, color="Set3", names=["kirk","spock","bones","scotty","uhura","sulu"])
plt.title("Seaborn violin plot in Bokeh")
# Convert to interactive Bokeh plot with one command
show(mpl.to_bokeh(name="violin"))
I believe that the issue is that I'm converting a figure from seaborn to matplotlib to bokeh, but I'm not sure at what level the x-axis labels go in.
I've confirmed that the labels are showing up in matplotlib before conversion to bokeh. I've also tried adding the labels to bokeh after conversion, but this results in a weird plot. I've created an issue for this problem with the bokeh developers here.
Since Bokeh 12.5 (April 2017), support for Matplotlib has been deprecated, so mpl.to_bokeh() is no longer available.

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