How make row size of seaborn clustermap equal? [duplicate] - python

When plotting heatmaps with seaborn (and correlation matrices with matplotlib) the first and the last row is cut in halve.
This happens also when I run this minimal code example which I found online.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.read_csv('https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv')
plt.figure(figsize=(10,5))
sns.heatmap(data.corr())
plt.show()
The labels at the y axis are on the correct spot, but the rows aren't completely there.
A few days ago, it work as intended. Since then, I installed texlive-xetex so I removed it again but it didn't solve my problem.
Any ideas what I could be missing?

Unfortunately matplotlib 3.1.1 broke seaborn heatmaps; and in general inverted axes with fixed ticks.
This is fixed in the current development version; you may hence
revert to matplotlib 3.1.0
use matplotlib 3.1.2 or higher
set the heatmap limits manually (ax.set_ylim(bottom, top) # set the ylim to bottom, top)

Its a bug in the matplotlib regression between 3.1.0 and 3.1.1
You can correct this by:
import seaborn as sns
df_corr = someDataFrame.corr()
ax = sns.heatmap(df_corr, annot=True) #notation: "annot" not "annote"
bottom, top = ax.get_ylim()
ax.set_ylim(bottom + 0.5, top - 0.5)

Fixed using the above and setting the heatmap limits manually.
First
ax = sns.heatmap(...
checked the current axes with
ax.get_ylim()
(5.5, 0.5)
Fixed with
ax.set_ylim(6.0, 0)

I solved it by adding this line in my code, with matplotlib==3.1.1:
ax.set_ylim(sorted(ax.get_xlim(), reverse=True))
NB. The only reason this works is because the x-axis isn't changed, so use at your own risk with future mpl versions

matplotlib 3.1.2 is out -
It is available in the Anaconda cloud via conda-forge but I was not able to install it via conda install.
The manual alternative worked:
Download matplotlib 3.1.2 from github and install via pip
% curl https://codeload.github.com/matplotlib/matplotlib/tar.gz/v3.1.2 --output matplotlib-3.1.2.tar.gz
% pip install matplotlib-3.1.2.tar.gz

Worked for me:
b, t = plt.ylim()
b += 0.5
t -= 0.5
custom_ylim = (b, t)
plt.setp(axes, ylim=custom_ylim)

It happens with matplotlib version 3.1.1 as suggested by importanceofbeingernest
Following solved my problem
pip install matplotlib==3.1.0

rustyDev is right about conda-forge, but I did not need to do a manual pip install from a github download. For me, on Windows, it worked directly. And the plots are all nice again.
https://anaconda.org/conda-forge/matplotlib
conda install -c conda-forge matplotlib
optional points, not needed for the answer:
Afterwards, I tried other steps, but they are not needed: In conda prompt: conda search matplotlib --info showed no new version info, the most recent info was for 3.1.1. Thus I tried pip using pip install matplotlib==3.1.2 But pip says "Requirement already satisfied"
Then getting the version according to medium.com/#rakshithvasudev/… python - import matplotlib - matplotlib.__version__ shows that 3.1.2 was successfully installed
Btw, I had this error directly after updating Spyder to v4.0.0. The error was in a plot of a confusion matrix. This was mentioned already some months ago. stackoverflow.com/questions/57225685/… which is already linked to this seaborn question.

Downgrade your matplotlib
!pip install matplotlib==3.1.0
and add this line to your plot code :
ax[i].set_ylim(sorted(ax[i].get_xlim(), reverse=True))

As #ImportanceOfBeingErnest mentioned, this issue is due to broken seaborn heatmaps in a specific version of matplotlib so simple solution to this problem is to upgrade matplotlib as follows:
pip install --upgrade matplotlib

Related

How do I run a Python Script in PowerBI?

I am new to Python. I am trying to create a Bar Chart in Power BI using Python but the script does not seem to work. The code is below:
pip install pandas
pip install matplotlib
import matplotlib.pyplot as plt
plt.plot(dataset.Letter,dataset.Number)
plt.show()
Please find attached the file:
https://www.mediafire.com/file/f0bwt9cp4ha1sfb/Example.pbix/file
enter image description here
After installing only one (1) initial time pip install pandas and pip install matplotlib, you only need to write in your code:
import matplotlib.pyplot as plt
dataset.plot('bar', dataset.Letter, dataset.Number)
plt.show()
import is for setting up the library and you must always use it in your code, while pip install pandas and pip install matplotlib only need to be installed once at the beginning.
First, you should not be required to run pip install every time you run your script. You should only have to use pip install commands when installing a library for the first time. This might require you ensuring that you have the correct libraries located on your computer. However, I do not know what your error is since I do not have your error code.
Also, be sure to specify what type of plot you wish to produce from matplotlob.
Try (EDIT):
import matplotlib.pyplot as plt
dataset.plot(kind='bar', x='Letter', y='Number')
plt.show()
You do not need an indent in your last two lines of code.
Also after reviewing you update I think it might be best if yo review the matplotlib library documentation and familiarize yourself with Python basics. You can learn Python almost anywhere, I have found the interactive lessons on w3schools.com to be useful.
matplotlib: https://matplotlib.org/stable/users/index.html

UserWarning: Matplotlib is currently using agg, so cannot show the figure

I'm trying to run a basic matplotlib example from the official website:
However, when i run the code, my Python interpreter complains and outputs the following message:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()
I've installed matplotlib via pip3 install matplotlib.
My current python3 version is 3.9.1 and my OS is Ubuntu 20.04.
I've already tried installing tkinter, as already described here, with no success.
What should I do? Why is this happening?
Please try these, if any works for you:
if you are using Jupyter Notebook
%matplotlib inline
make sure you have tkinter, recompile python interpreter after installing tkinter
try:
import matplotlib
import matplotlib.pyplot as plt
plt.style.use('ggplot')
matplotlib.use( 'tkagg' )
x = [1, 5, 1.5, 4]
y = [9, 1.8, 8, 11]
plt.scatter(x,y)
plt.show()

How to fix cells cut in Seaborn clustermap [duplicate]

When plotting heatmaps with seaborn (and correlation matrices with matplotlib) the first and the last row is cut in halve.
This happens also when I run this minimal code example which I found online.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.read_csv('https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv')
plt.figure(figsize=(10,5))
sns.heatmap(data.corr())
plt.show()
The labels at the y axis are on the correct spot, but the rows aren't completely there.
A few days ago, it work as intended. Since then, I installed texlive-xetex so I removed it again but it didn't solve my problem.
Any ideas what I could be missing?
Unfortunately matplotlib 3.1.1 broke seaborn heatmaps; and in general inverted axes with fixed ticks.
This is fixed in the current development version; you may hence
revert to matplotlib 3.1.0
use matplotlib 3.1.2 or higher
set the heatmap limits manually (ax.set_ylim(bottom, top) # set the ylim to bottom, top)
Its a bug in the matplotlib regression between 3.1.0 and 3.1.1
You can correct this by:
import seaborn as sns
df_corr = someDataFrame.corr()
ax = sns.heatmap(df_corr, annot=True) #notation: "annot" not "annote"
bottom, top = ax.get_ylim()
ax.set_ylim(bottom + 0.5, top - 0.5)
Fixed using the above and setting the heatmap limits manually.
First
ax = sns.heatmap(...
checked the current axes with
ax.get_ylim()
(5.5, 0.5)
Fixed with
ax.set_ylim(6.0, 0)
I solved it by adding this line in my code, with matplotlib==3.1.1:
ax.set_ylim(sorted(ax.get_xlim(), reverse=True))
NB. The only reason this works is because the x-axis isn't changed, so use at your own risk with future mpl versions
matplotlib 3.1.2 is out -
It is available in the Anaconda cloud via conda-forge but I was not able to install it via conda install.
The manual alternative worked:
Download matplotlib 3.1.2 from github and install via pip
% curl https://codeload.github.com/matplotlib/matplotlib/tar.gz/v3.1.2 --output matplotlib-3.1.2.tar.gz
% pip install matplotlib-3.1.2.tar.gz
Worked for me:
b, t = plt.ylim()
b += 0.5
t -= 0.5
custom_ylim = (b, t)
plt.setp(axes, ylim=custom_ylim)
It happens with matplotlib version 3.1.1 as suggested by importanceofbeingernest
Following solved my problem
pip install matplotlib==3.1.0
rustyDev is right about conda-forge, but I did not need to do a manual pip install from a github download. For me, on Windows, it worked directly. And the plots are all nice again.
https://anaconda.org/conda-forge/matplotlib
conda install -c conda-forge matplotlib
optional points, not needed for the answer:
Afterwards, I tried other steps, but they are not needed: In conda prompt: conda search matplotlib --info showed no new version info, the most recent info was for 3.1.1. Thus I tried pip using pip install matplotlib==3.1.2 But pip says "Requirement already satisfied"
Then getting the version according to medium.com/#rakshithvasudev/… python - import matplotlib - matplotlib.__version__ shows that 3.1.2 was successfully installed
Btw, I had this error directly after updating Spyder to v4.0.0. The error was in a plot of a confusion matrix. This was mentioned already some months ago. stackoverflow.com/questions/57225685/… which is already linked to this seaborn question.
Downgrade your matplotlib
!pip install matplotlib==3.1.0
and add this line to your plot code :
ax[i].set_ylim(sorted(ax[i].get_xlim(), reverse=True))
As #ImportanceOfBeingErnest mentioned, this issue is due to broken seaborn heatmaps in a specific version of matplotlib so simple solution to this problem is to upgrade matplotlib as follows:
pip install --upgrade matplotlib

Anyone cannot plot using seaborn's relplot()?

Hi all,
I have updated the latest seaborn version, however, when I typed into the relplot() function to plot the scatterpoint, it was shown as no attribute.
Any clue?
Was having the same problem. I ran this:
pip install --upgrade seaborn
then restarted the kernel; relplot() works fine now.

Basemap with Python 3.5 Anaconda on Windows

I use Python 3.5 with latest version of Anaconda on Windows (64 bit). I wanted to install Basemap using conda install basemap. Apparently there is a conflict between Python 3 and basemap. After some googling indeed I found that basemap is not supported on Python 3 for Windows users (ex: https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/TjAwi3ilQaU).
For obvious reasons I do not want to downgrade to Python 2. What would then be the simplest alternative solution?
Is there an alternative package similar to basemap for ploting maps, etc.?
Should I use a second environment which uses Python 2 and basemap? I have never done that but it seems possible (http://conda.pydata.org/docs/py2or3.html). Is it "safe"? Should I install again all the other packages (matplotlib, numpy, etc.) on the second environment?
Thanks in advance for the help and advice.
Referring to the answer of Solly, I have Windows 10, python 3.5.3, Anaconda 64bit, in the Anaconda prompt I entered:
conda install -c conda-forge basemap=1.0.8.dev0
conda install -c conda-forge basemap-data-hires
then the code, taken from Python for Data Science for Dummies, page 193 (Plotting geographical data worked just fine.
I wanted to add just a comment to the Solly's answer, but I don't have enough credits to do so.
The code is:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
austin = (-97.75, 30.25)
hawaii = (-157.8, 21.3)
washington = (-77.01, 38.90)
chicago = (-87.68, 41.83)
losangeles = (-118.25, 34.05)
m = Basemap(projection = 'merc', llcrnrlat=10, urcrnrlat=50,
llcrnrlon=-160, urcrnrlon=-60)
m.drawcoastlines()
m.fillcontinents (color='lightgray', lake_color='lightblue')
m.drawparallels(np.arange(-90.,91.,30.))
m.drawmeridians(np.arange(-180.,181.,60.))
m.drawmapboundary(fill_color='aqua')
m.drawcounties()
x, y = m(*zip(*[hawaii, austin, washington, chicago, losangeles]))
m.plot(x,y, marker ='o', markersize=6, markerfacecolor='red', linewidth=0)
plt.title('Mercator Projection')
plt.show()
I have solved this several times (last time just now) by downloading it from
http://www.lfd.uci.edu/~gohlke/pythonlibs
and follow the instructions to install. From the anaconda command prompt
pip install full_path_to_package
For example, if you downloaded basemap-1.1.0-cp36-cp36m-win_amd64.whl, you would run
pip install C:\path\to\file\basemap-1.1.0-cp36-cp36m-win_amd64.whl
Note that the python version of the .whl file must match your python version. For example, ...-cp36-.... indicates Python 3.6. You can find your python version by running the command python --version.
I was running in the same problem (Python 3.5 and Anaconda) and eventually downloaded Basemap 1.0.8dev0 from here and installed it using conda (as described by the link).
Cartopy is an alternative to Basemap, and it is being actively developed.
There is a nice gallery here:
http://scitools.org.uk/cartopy/docs/latest/gallery.html
The below information is for Mac OS:
Downloaded from here!
Run conda install -c conda-forge basemap-1.2.0-py37h9622e30_3.tar.bz2
Done
Truth be told I had the same problem and tried to fix it for waaay to long and even tried a python 2 environment with no luck.
Personally just using a python 2 install was way easier and less time consuming. Sorry for the non answer.

Categories