How do you save Matplotlib figure with an opaque white border? - python

When saving a Matplotlib figure from a Jupyter notebook, how do I override the default transparent border so it will be opaque?
Looking at the savefig documentation, there are several parameters that seem like they would affect this but actually do not seem to do anything. Here is an example.
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = np.linspace(-6, 6, 100)
ax.plot(x, np.sinc(x))
plt.savefig(
'test.png',
transparent=False, # no change
frameon=True, # no change
edgecolor='blue', # no change (want 'white' but 'blue' should be noticeable)
facecolor='red', # no change (want 'white' but 'red' should be noticeable)
alpha=1, # no change
)
Here is the result. StackOverflow does not illustrate the transparency, but note that the edge is not 'blue' and the face is not 'red'.

This post mentions setting fig.patch.set_alpha(1) which turns out to work, regardless of the savefig parameters. Adding this command to the example code resolves the problem.
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.patch.set_alpha(1) # solution
x = np.linspace(-6, 6, 100)
ax.plot(x, np.sinc(x))
fig.savefig('solved.png')
It turns out this is specific to Jupyter notebooks, and is probably a bug (I only have version 4.4.0). When I run the following code above from the command line, I instead get the desired behavior (change 'red' to 'white' to get the opaque white border).
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = np.linspace(-6, 6, 100)
ax.plot(x, np.sinc(x))
plt.savefig(
'test.png',
# transparent=False, # no change
# frameon=True, # no change
# edgecolor='blue', # no change
facecolor='red', # no change
# alpha=1, # no change
)

Related

How do I put a watermark behind plotted data using matplotlib

I found this tutorial on how to do a watermark but I cannot figure out how to put it behind my plotted data.
https://www.tutorialspoint.com/how-to-plot-a-watermark-image-in-matplotlib
Changing zorder has no impact because I think it is being drawn on the entire figure. I would like to have a subdued logo behind my data which is always centered in the figure so I don't really want to plot an image as a data point because then it would move as it is panned/zoomed.
Setting the zorder to a negative value works for me. However, you also need to make the facecolor of the axes transparent:
import numpy as np
import matplotlib.cbook as cbook
import matplotlib.image as image
import matplotlib.pyplot as plt
with cbook.get_sample_data('logo2.png') as file:
im = image.imread(file)
fig, ax = plt.subplots()
fig.figimage(im, 10, 10, zorder=-1, alpha=.5)
ax.plot(np.sin(10 * np.linspace(0, 1)), '-o', ms=20,
alpha=0.7, mfc='orange')
ax.set_facecolor('none')
plt.show()

Removing legend from mpl parallel coordinates plot?

I have a parallel coordinates plot with lots of data points so I'm trying to use a continuous colour bar to represent that, which I think I have worked out. However, I haven't been able to remove the default key that is put in when creating the plot, which is very long and hinders readability. Is there a way to remove this table to make the graph much easier to read?
This is the code I'm currently using to generate the parallel coordinates plot:
parallel_coordinates(data[[' male_le','
female_le','diet','activity','obese_perc','median_income']],'median_income',colormap = 'rainbow',
alpha = 0.5)
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cmap = mpl.cm.rainbow
bounds = [0.00,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N,)
plt.colorbar(mpl.cm.ScalarMappable(norm = norm, cmap=cmap),cax = ax, orientation = 'horizontal',
label = 'normalised median income', alpha = 0.5)
plt.show()
Current Output:
I want my legend to be represented as a color bar, like this:
Any help would be greatly appreciated. Thanks.
You can use ax.legend_.remove() to remove the legend.
The cax parameter of plt.colorbar indicates the subplot where to put the colorbar. If you leave it out, matplotlib will create a new subplot, "stealing" space from the current subplot (subplots are often referenced to by ax in matplotlib). So, here leaving out cax (adding ax=ax isn't necessary, as here ax is the current subplot) will create the desired colorbar.
The code below uses seaborn's penguin dataset to create a standalone example.
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
import numpy as np
from pandas.plotting import parallel_coordinates
penguins = sns.load_dataset('penguins')
fig, ax = plt.subplots(figsize=(10, 4))
cmap = plt.get_cmap('rainbow')
bounds = np.arange(penguins['body_mass_g'].min(), penguins['body_mass_g'].max() + 200, 200)
norm = mpl.colors.BoundaryNorm(bounds, 256)
penguins = penguins.dropna(subset=['body_mass_g'])
parallel_coordinates(penguins[['bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', 'body_mass_g']],
'body_mass_g', colormap=cmap, alpha=0.5, ax=ax)
ax.legend_.remove()
plt.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
ax=ax, orientation='horizontal', label='body mass', alpha=0.5)
plt.show()

How to have a image as the plot face (the outer border) in matplotlib?

I know I can change the color using fig.patch.set_facecolor("#ccdece") but how do I have an image instead of a solid color? Like using img = plt.imread() and ax.imshow(img) but for the outer border.
Any help is welcome.
You can create a dummy ax for the full size of the surrounding figure and add an image to that ax. Giving the ax a low enough zorder makes sure it appears behind the actual plots.
For an additional effect, the facecolor of the actual plots can be made semi-transparent.
Here is an example starting from a stock image.
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import numpy as np
imageFile = cbook.get_sample_data('ada.png')
image = plt.imread(imageFile)
fig, ax = plt.subplots(figsize=(6, 8))
bg_ax = fig.add_axes([0, 0, 1, 1], zorder=-1)
bg_ax.axis('off')
bg_ax.imshow(image)
t = np.linspace(0, 4 * np.pi, 200)
x = 2 * np.cos(t / 2)
y = np.sin(t)
ax.plot(x, y)
ax.set_facecolor('#FFFFFFEE')
plt.show()

matplotlib: figimage not showing in Jupyter notebook

I'm trying to render an image at its true dimensions (not scaled or stretched) and the easiest way to do this with matplotlib seems to be figimage.
However, when I try to use it in a Jupyter notebook, the figure doesn't show. Other plots show fine, this only seems to affect figimage:
As you can see, this first plot shows fine, but the second one does not. What am I doing wrong?
When I run the following code in an IPython shell , the figure shows up as expected, so maybe it's a problem with my Jupyter setup?
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 500)
plt.plot(x, np.sin(x))
plt.show()
data = np.random.random((500,500))
plt.figimage(data)
plt.show()
figimage only adds a background to the current figure. If you don't have an already existing figure, the command wont render anything. The following snippet will work both inside and outside IPython Notebook:
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x = np.linspace(0, 2 * np.pi, 500)
plt.plot(x, np.sin(x))
data = np.random.randn(500, 500)
plt.figimage(data)
plt.show()
However, it doesn't do what you want/expect. In order to render an image in its true dimensions you would have to play with figsize and dpi, as others have attempted previously [1] [2] [3] [4]:
data = np.random.randn(500, 500)
dpi = 80
shape = data.shape
fig, ax = plt.subplots(figsize=(shape[1]/float(dpi), shape[0]/float(dpi)), dpi=dpi, frameon=False)
ax.imshow(data, extent=(0,1,1,0))
ax.set_xticks([]) # remove xticks
ax.set_yticks([]) # remove yticks
ax.axis('off') # hide axis
fig.subplots_adjust(bottom=0, top=1, left=0, right=1, wspace=0, hspace=0) # streches the image and removes margins
fig.savefig('/tmp/random.png', dpi=dpi, pad_inches=0, transparent=True) # Optional: save figure
fig.show()

Seaborn, change font size of the colorbar

I wanted to change the font size for a heatmap colorbar.
The following is my code:
import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
plt.show()
I was able to change the tick labels with plt.tick_params(axis='both', labelsize=20). However, the colorbar font size does not change.
Is there a way to do that?
You can use matplotlib.axes.Axes.tick_params with labelsize.
For example, your plot with labelsize 20:
import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
# use matplotlib.colorbar.Colorbar object
cbar = ax.collections[0].colorbar
# here set the labelsize by 20
cbar.ax.tick_params(labelsize=20)
plt.show()
I refered to the following answer:
 - Using matplotlib.colorbar.Colorbar object
 - Setting parameter
You can change the font scale with the seaborn.set() method setting the font_scale param to the scale you want, see more in seaborn documentation.
For example, your plot with scale 3:
import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
# here set the scale by 3
sns.set(font_scale=3)
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
plt.show()
If you set the following, it will increase all text in the graph by a factor of two. However, if you immediately set the tick_params to lower right after, you will be left with just the font size of the colorbar increased.
sns.set(font_scale=2)
sns.heatmap(df, vmin=0, vmax=1, center=0.5)
heatmap.tick_params(labelsize=15)
sns.set(font_scale=1)
Don't forget to set the font_scale back :)

Categories

Resources