matplotlib, seaborn version solving failed - matplotlib

I am installing matplotlib, seaborn using poetry(poetry add matplotlib seaborn) but getting following error as below:
Because no versions of seaborn match >0.12.1,<0.13.0
and seaborn (0.12.1) depends on matplotlib (>=3.1,<3.6.1 || >3.6.1), seaborn (>=0.12.1,<0.13.0) requires matplotlib (>=3.1,<3.6.1 || >3.6.1).
And because no versions of matplotlib match >3.6.1,<4.0.0, seaborn (>=0.12.1,<0.13.0) requires matplotlib (>=3.1,<3.6.1 || >=4.0.0).
So, because project depends on both matplotlib (^3.6.1) and seaborn (^0.12.1), version solving failed.
what I am missing?
I was able to install either matplotlib or seaborn, But not both.
As I need both libraries, how should I install both using poetry?

As the error says, seaborn (0.12.1) depends on matplotlib (>=3.1,<3.6.1 || >3.6.1), i.e., >=3.1, != 3.6.1.
So you chould specify your environment as matplotlib ^3.6.0 and seaborn ^0.12.1.

Related

Matplotlib fonts not found in Google Colab [duplicate]

Using custom fonts in matplotlib locally involves storing the .ttfs in the matplotlib/mpl-data/fonts/ttf/ folder, then calling mpl.font_manager._rebuild(), then setting mpl.rcParams['font.sans-serif'].
Is there any way to do this in Google Colaboratory, where it doesn't seem that this ttf folder is accessible?
For example, I'd like to use the Roboto font. After installing, this would be invoked using mpl.rcParams['font.sans-serif'] = 'Roboto'.
The ttf folder is here:
/usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
So you want to download the ttf there, e.g.:
!wget https://github.com/Phonbopit/sarabun-webfont/raw/master/fonts/thsarabunnew-webfont.ttf -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
matplotlib.font_manager._rebuild()
matplotlib.rc('font', family='TH Sarabun New')
update 2019-12
_rebuild() no longer works. Here's another method which still works.
import matplotlib
import matplotlib.font_manager as fm
!wget https://github.com/Phonbopit/sarabun-webfont/raw/master/fonts/thsarabunnew-webfont.ttf
fm.fontManager.ttflist += fm.createFontList(['thsarabunnew-webfont.ttf'])
matplotlib.rc('font', family='TH Sarabun New')
Wanted to add a full, succinct answer that currently works.
# Download fonts of choice. Here we download Open Sans variants to
# the current directory.
# It's not necessary to download to the share or matplotlib folders:
# /usr/share/fonts/truetype
# /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Regular.ttf'
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Light.ttf'
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-SemiBold.ttf'
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Bold.ttf'
from matplotlib import font_manager as fm, pyplot as plt
# Pick up any fonts in the current directory.
# If you do end up downloading the fonts to /usr/share/fonts/truetype,
# change this to: fm.findSystemFonts()
font_files = fm.findSystemFonts('.')
# Go through and add each to Matplotlib's font cache.
for font_file in font_files:
fm.fontManager.addfont(font_file)
# Use your new font on all your plots.
plt.rc('font', family='Open Sans')
Note, a few times this didn't work properly and the requested font wasn't displayed (even though no error or warning was shown). If that happens, try factory resetting your Colab runtime and running again.
When matplotlib 3.2 is released, it will be easier.
# For now we must upgrade to 3.2 rc first
# !pip install -U --pre matplotlib
import matplotlib as mpl
mpl.font_manager.fontManager.addfont('thsarabunnew-webfont.ttf')
mpl.rc('font', family='TH Sarabun New')
I would like to add my solutions as another reference:
Change the seaborn style
I would strongly recommend this approach as changing the font family can be very troublesome and inconvenient per seaborn design (many of the posts are no longer working on my end in 2022/05). So if you just want to get rid of the stupid default font in matplotlib and seaborn and is OK with Arial, go and type
%matplotlib inline
import matplotlib.style as style
style.use('seaborn-deep')
Changing the font type (borrowed from top answers and tested myself. Restarting the runtime several times if it is not working as expected)
import matplotlib as mpl
import matplotlib.font_manager as fm
from matplotlib import font_manager as fm, pyplot as plt
!wget https://github.com/trishume/OpenTuringCompiler/blob/master/stdlib-sfml/fonts/Times%20New%20Roman.ttf
!wget https://github.com/matomo-org/travis-scripts/blob/master/fonts/Arial.ttf
font_files = fm.findSystemFonts()
# Go through and add each to Matplotlib's font cache.
for font_file in font_files:
fm.fontManager.addfont(font_file)
fm.fontManager.ttflist += fm.createFontList(['Times New Roman.ttf'])
# Use your new font on all your plots.
plt.rc('font', family='serif')
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.show()
Updates on 2022/06/08: Method 1 sometimes doesn't work out in Colab, but it works on the local Jupyter Notebook. It seems that explicitly installing and adding the font types is the only way if you want to customize font types on Colab.

seaborn "kde jointplot" doesn't have color mapping in the latest version (0.11.0)

I was running seaborn ver. 0.10.1 on my jupyter notebook. This morning I upgraded to the latest version 0.11.0. Now, my kde jointplot doesn't give the color mapping that it used to. The code is the same. Only the versions are different.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib notebook
np.random.seed(1234)
v1 = pd.Series(np.random.normal(0,10,1000), name='v1')
v2 = pd.Series(np.random.normal(60,15,1000), name='v2')
v3 = pd.Series(2*v1 + v2, name='v3')
# set the seaborn style for all the following plots
sns.set_style('white')
sns.jointplot(v1, v3, kind='kde', space=0);
The function kdeplot (which is used internally by jointplot()to draw the bivariate density plot) has been extensively changed in v.0.11. See What's new and the documentation.
You now have to pass fill=True to get a filled KDE, and you need to specify thresh=0 if you want to fill the available space with color.
sns.jointplot(x=v1, y=v3, kind='kde', space=0, fill=True, thresh=0, cmap='Blues');

Custom fonts in Google Colaboratory matplotlib charts

Using custom fonts in matplotlib locally involves storing the .ttfs in the matplotlib/mpl-data/fonts/ttf/ folder, then calling mpl.font_manager._rebuild(), then setting mpl.rcParams['font.sans-serif'].
Is there any way to do this in Google Colaboratory, where it doesn't seem that this ttf folder is accessible?
For example, I'd like to use the Roboto font. After installing, this would be invoked using mpl.rcParams['font.sans-serif'] = 'Roboto'.
The ttf folder is here:
/usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
So you want to download the ttf there, e.g.:
!wget https://github.com/Phonbopit/sarabun-webfont/raw/master/fonts/thsarabunnew-webfont.ttf -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
matplotlib.font_manager._rebuild()
matplotlib.rc('font', family='TH Sarabun New')
update 2019-12
_rebuild() no longer works. Here's another method which still works.
import matplotlib
import matplotlib.font_manager as fm
!wget https://github.com/Phonbopit/sarabun-webfont/raw/master/fonts/thsarabunnew-webfont.ttf
fm.fontManager.ttflist += fm.createFontList(['thsarabunnew-webfont.ttf'])
matplotlib.rc('font', family='TH Sarabun New')
Wanted to add a full, succinct answer that currently works.
# Download fonts of choice. Here we download Open Sans variants to
# the current directory.
# It's not necessary to download to the share or matplotlib folders:
# /usr/share/fonts/truetype
# /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Regular.ttf'
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Light.ttf'
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-SemiBold.ttf'
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Bold.ttf'
from matplotlib import font_manager as fm, pyplot as plt
# Pick up any fonts in the current directory.
# If you do end up downloading the fonts to /usr/share/fonts/truetype,
# change this to: fm.findSystemFonts()
font_files = fm.findSystemFonts('.')
# Go through and add each to Matplotlib's font cache.
for font_file in font_files:
fm.fontManager.addfont(font_file)
# Use your new font on all your plots.
plt.rc('font', family='Open Sans')
Note, a few times this didn't work properly and the requested font wasn't displayed (even though no error or warning was shown). If that happens, try factory resetting your Colab runtime and running again.
When matplotlib 3.2 is released, it will be easier.
# For now we must upgrade to 3.2 rc first
# !pip install -U --pre matplotlib
import matplotlib as mpl
mpl.font_manager.fontManager.addfont('thsarabunnew-webfont.ttf')
mpl.rc('font', family='TH Sarabun New')
I would like to add my solutions as another reference:
Change the seaborn style
I would strongly recommend this approach as changing the font family can be very troublesome and inconvenient per seaborn design (many of the posts are no longer working on my end in 2022/05). So if you just want to get rid of the stupid default font in matplotlib and seaborn and is OK with Arial, go and type
%matplotlib inline
import matplotlib.style as style
style.use('seaborn-deep')
Changing the font type (borrowed from top answers and tested myself. Restarting the runtime several times if it is not working as expected)
import matplotlib as mpl
import matplotlib.font_manager as fm
from matplotlib import font_manager as fm, pyplot as plt
!wget https://github.com/trishume/OpenTuringCompiler/blob/master/stdlib-sfml/fonts/Times%20New%20Roman.ttf
!wget https://github.com/matomo-org/travis-scripts/blob/master/fonts/Arial.ttf
font_files = fm.findSystemFonts()
# Go through and add each to Matplotlib's font cache.
for font_file in font_files:
fm.fontManager.addfont(font_file)
fm.fontManager.ttflist += fm.createFontList(['Times New Roman.ttf'])
# Use your new font on all your plots.
plt.rc('font', family='serif')
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.show()
Updates on 2022/06/08: Method 1 sometimes doesn't work out in Colab, but it works on the local Jupyter Notebook. It seems that explicitly installing and adding the font types is the only way if you want to customize font types on Colab.

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