import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns;
tips = sns.load_dataset('tips')
tips.head()
tips['tip_pct'] = 100 * tips['tip'] / tips['total_bill']
grid = sns.FacetGrid(tips, row="sex", col="time", margin_titles=True)
grid.map(plt.hist, "tip_pct", bins=np.linspace(0, 40, 15));
When I run the above code in Spyder IDE (Anaconda Navigator's Package), I get the desired results. But when the same code is run in Jupter QtConsole (including the line: %matplotlib inline) I get the following errors:
Out:
ValueErrorTraceback (most recent call last)
<ipython-input-47-c7ea1bbe0c80> in <module>()
----> 1 grid.map(plt.hist, "tip_pct", bins=np.linspace(0, 40, 15));
/Users/waqas/anaconda/lib/python3.5/site-packages/seaborn/axisgrid.py in map(self, func, *args, **kwargs)
701
702 # Get the current axis
--> 703 ax = self.facet_axis(row_i, col_j)
704
705 # Decide what color to plot with
/Users/waqas/anaconda/lib/python3.5/site-packages/seaborn/axisgrid.py in facet_axis(self, row_i, col_j)
832
833 # Get a reference to the axes object we want, and make it active
--> 834 plt.sca(ax)
835 return ax
836
/Users/waqas/anaconda/lib/python3.5/site-packages/matplotlib/pyplot.py in sca(ax)
905 m.canvas.figure.sca(ax)
906 return
--> 907 raise ValueError("Axes instance argument was not found in a figure.")
908
909
ValueError: Axes instance argument was not found in a figure.
I don't know what's going on.
Somewhat related...I was getting the same error when running jupyter notebook because I was running the following lines in separate cells.
g = sns.FacetGrid(data=titanic,col='sex')
g.map(plt.hist,'age')
Once I ran them both in the same cell the image displayed properly.
Since you're using Qt console see if it helps to assign your mapping to grid.
grid = grid.map(plt.hist, "tip_pct", bins=np.linspace(0, 40, 15))
You'll see the same approach is used in the documentation for FacetGrid.
Related
I am trying to generate a correlation matrix using corr()
from datascience import *
import pandas as pd
import numpy as np
import scipy
import matplotlib.pyplot as plots
plots.style.use ("fivethirtyeight")
premier = Table.read_table("Documents//Stats PL1.csv")
premier.corr()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-21-9531b81a92c8> in <module>
----> 1 premier.corr()
~\anaconda3\lib\site-packages\datascience\tables.py in __getattr__(self, attr)
233 else:
234 msg = "'{0}' object has no attribute '{1}'".format(type(self).__name__, attr)
--> 235 raise AttributeError(msg)
236
237 ####################
AttributeError: 'Table' object has no attribute 'corr'
I already review all the pandas instructions, installed and unistalled all the packages, this is not the first function that fail (the other one was df.to_excel) but didnĀ“t solved it either
Realy dont know where to look for an answer, thanks for all the help!
I am working with some neural networks and I am struggling to plot a correlation heatmap for the titanic dataset using seaborn. To be concise: it seems that there is a problem with the 'n_siblings_spouses' features during the plotting. I don't know if the problem is due to the feature itself (spacing, maybe?) or if there is an intrinsic issue with seaborn.
Would it be possible to solve the issue without the need to remove the feature from the dataset?
Here is a MWE. And thanks in advance!
from __future__ import absolute_import,division,print_function,unicode_literals
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import rc, font_manager
%matplotlib inline
from IPython.display import clear_output
from six.moves import urllib
import tensorflow.compat.v2.feature_column as fc
import tensorflow as tf
import seaborn as sns
rc('text', usetex=True)
matplotlib.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']
# only if needed
#!apt install texlive-fonts-recommended texlive-fonts-extra cm-super dvipng
plt.rc('font', family='serif')
# URL address of data
TRAIN_DATA_URL = "https://storage.googleapis.com/tf-datasets/titanic/train.csv"
# Downloading data
train_file_path = tf.keras.utils.get_file("train.csv", TRAIN_DATA_URL)
# Setting numpy default values.
np.set_printoptions(precision=3, suppress=True)
# Reading data
data_train = pd.read_csv(train_file_path)
print("\n TRAIN DATA SET")
print(data_train.head(),"\n")
def heatMap(df):
#Create Correlation df
corr = df.corr()
#Plot figsize
fig, ax = plt.subplots(figsize=(10, 10))
#Generate Color Map
colormap = sns.diverging_palette(220, 10, as_cmap=True)
#Generate Heat Map, allow annotations and place floats in map
sns.heatmap(corr, cmap=colormap, annot=True, fmt=".2f")
#Apply xticks
plt.xticks(range(len(corr.columns)), corr.columns);
#Apply yticks
plt.yticks(range(len(corr.columns)), corr.columns)
#show plot
plt.show()
heatMap(data_train)
Here is the issue that is raised when trying to execute the heatMap function (I am working in Colab. However, this also happens in console):
---------------------------------------------------------------------------
CalledProcessError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/matplotlib/texmanager.py in _run_checked_subprocess(self, command, tex)
305 cwd=self.texcache,
--> 306 stderr=subprocess.STDOUT)
307 except FileNotFoundError as exc:
22 frames
CalledProcessError: Command '['latex', '-interaction=nonstopmode', '--halt-on-error', '/root/.cache/matplotlib/tex.cache/bf616eae1512bede263889c8e1d8fb21.tex']' returned non-zero exit status 1.
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/matplotlib/texmanager.py in _run_checked_subprocess(self, command, tex)
317 prog=command[0],
318 tex=tex.encode('unicode_escape'),
--> 319 exc=exc.output.decode('utf-8'))) from exc
320 _log.debug(report)
321 return report
RuntimeError: latex was not able to process the following string:
b'n_siblings_spouses'
Here is the full report generated by latex:
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=latex)
restricted \write18 enabled.
entering extended mode
(/root/.cache/matplotlib/tex.cache/bf616eae1512bede263889c8e1d8fb21.tex
LaTeX2e <2017-04-15>
Babel <3.18> and hyphenation patterns for 3 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/usr/share/texmf/tex/latex/cm-super/type1ec.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/t1cmr.fd))
(/usr/share/texlive/texmf-dist/tex/latex/base/textcomp.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.def))
(/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/utf8.def
(/usr/share/texlive/texmf-dist/tex/latex/base/t1enc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/ot1enc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/omsenc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.dfu)))
(/usr/share/texlive/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(/usr/share/texlive/texmf-dist/tex/generic/ifxetex/ifxetex.sty)
Package geometry Warning: Over-specification in `h'-direction.
`width' (5058.9pt) is ignored.
Package geometry Warning: Over-specification in `v'-direction.
`height' (5058.9pt) is ignored.
) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty))
(./bf616eae1512bede263889c8e1d8fb21.aux)
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1cmr.fd)
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
! Missing $ inserted.
<inserted text>
$
l.19 {\rmfamily n_
siblings_spouses}
No pages of output.
Transcript written on bf616eae1512bede263889c8e1d8fb21.log.
---------------------------------------------------------------------------
CalledProcessError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/matplotlib/texmanager.py in _run_checked_subprocess(self, command, tex)
305 cwd=self.texcache,
--> 306 stderr=subprocess.STDOUT)
307 except FileNotFoundError as exc:
21 frames
CalledProcessError: Command '['latex', '-interaction=nonstopmode', '--halt-on-error', '/root/.cache/matplotlib/tex.cache/bf616eae1512bede263889c8e1d8fb21.tex']' returned non-zero exit status 1.
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/matplotlib/texmanager.py in _run_checked_subprocess(self, command, tex)
317 prog=command[0],
318 tex=tex.encode('unicode_escape'),
--> 319 exc=exc.output.decode('utf-8'))) from exc
320 _log.debug(report)
321 return report
RuntimeError: latex was not able to process the following string:
b'n_siblings_spouses'
Here is the full report generated by latex:
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=latex)
restricted \write18 enabled.
entering extended mode
(/root/.cache/matplotlib/tex.cache/bf616eae1512bede263889c8e1d8fb21.tex
LaTeX2e <2017-04-15>
Babel <3.18> and hyphenation patterns for 3 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/usr/share/texmf/tex/latex/cm-super/type1ec.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/t1cmr.fd))
(/usr/share/texlive/texmf-dist/tex/latex/base/textcomp.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.def))
(/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/utf8.def
(/usr/share/texlive/texmf-dist/tex/latex/base/t1enc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/ot1enc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/omsenc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.dfu)))
(/usr/share/texlive/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(/usr/share/texlive/texmf-dist/tex/generic/ifxetex/ifxetex.sty)
Package geometry Warning: Over-specification in `h'-direction.
`width' (5058.9pt) is ignored.
Package geometry Warning: Over-specification in `v'-direction.
`height' (5058.9pt) is ignored.
) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty))
(./bf616eae1512bede263889c8e1d8fb21.aux)
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1cmr.fd)
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
! Missing $ inserted.
<inserted text>
$
l.19 {\rmfamily n_
siblings_spouses}
No pages of output.
Transcript written on bf616eae1512bede263889c8e1d8fb21.log.
<Figure size 720x720 with 2 Axes>
To solve this problem, I came across this information that Colab needs a Tex-related module. There was also an excellent answer to SO.
You will need to install the following
! sudo apt-get install texlive-latex-recommended
! sudo apt-get install dvipng texlive-fonts-recommended
! wget http://mirrors.ctan.org/macros/latex/contrib/type1cm.zip
! unzip type1cm.zip -d /tmp/type1cm
! cd /tmp/type1cm/type1cm/ && sudo latex type1cm.ins
! sudo mkdir /usr/share/texmf/tex/latex/type1cm
! sudo cp /tmp/type1cm/type1cm/type1cm.sty /usr/share/texmf/tex/latex/type1cm
! sudo texhash
! sudo apt install cm-super
from __future__ import absolute_import,division,print_function,unicode_literals
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
# from matplotlib import rc, font_manager
%matplotlib inline
from IPython.display import clear_output
from six.moves import urllib
import tensorflow.compat.v2.feature_column as fc
import tensorflow as tf
import seaborn as sns
# rc('text', usetex=True)
# matplotlib.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']
# only if needed
#!apt install texlive-fonts-recommended texlive-fonts-extra cm-super dvipng
# plt.rc('font', family='serif')
# URL address of data
TRAIN_DATA_URL = "https://storage.googleapis.com/tf-datasets/titanic/train.csv"
# Downloading data
train_file_path = tf.keras.utils.get_file("/content/sample_data/train.csv", TRAIN_DATA_URL)
# Setting numpy default values.
np.set_printoptions(precision=3, suppress=True)
# Reading data
data_train = pd.read_csv(train_file_path)
print("\n TRAIN DATA SET")
print(data_train.head(),"\n")
def heatMap(df):
#Create Correlation df
corr = df.corr()
print(corr)
#Plot figsize
fig, ax = plt.subplots(figsize=(10, 10))
#Generate Color Map
colormap = sns.diverging_palette(220, 10, as_cmap=True)
#Generate Heat Map, allow annotations and place floats in map
sns.heatmap(corr, cmap=colormap, annot=True, fmt=".2f")
#Apply xticks
plt.xticks(range(len(corr.columns)), corr.columns);
#Apply yticks
plt.yticks(range(len(corr.columns)), corr.columns)
#show plot
plt.show()
heatMap(data_train)
I am running a Data8 instance of JupyterHub running JupyterLab on a server, and pd.read_clipboard() does not seem to work. I see the same problem in google colab.
import pandas as pd
pd.read_clipboard()
errors out like so:
---------------------------------------------------------------------------
PyperclipException Traceback (most recent call last)
<ipython-input-2-8cbad928c47b> in <module>()
----> 1 pd.read_clipboard()
/opt/conda/lib/python3.6/site-packages/pandas/io/clipboards.py in read_clipboard(sep, **kwargs)
29 from pandas.io.clipboard import clipboard_get
30 from pandas.io.parsers import read_table
---> 31 text = clipboard_get()
32
33 # try to decode (if needed on PY3)
/opt/conda/lib/python3.6/site-packages/pandas/io/clipboard/clipboards.py in __call__(self, *args, **kwargs)
125
126 def __call__(self, *args, **kwargs):
--> 127 raise PyperclipException(EXCEPT_MSG)
128
129 if PY2:
PyperclipException:
Pyperclip could not find a copy/paste mechanism for your system.
For more information, please visit https://pyperclip.readthedocs.org
Is there a way to get this working?
No. The machine is run in the cloud. Python from there cannot access your local machine to get clipboard content.
I tried Javascript clipboad api, but it didn't work probably because the output is in an iframe which isn't allow access to clipboard either. If it did, this would have worked
from google.colab.output import eval_js
text = eval_js("navigator.clipboard.readText()")
I'm trying to plot some graphs using the latest version of Pycharm as a Python IDE.
As an interpreter, I'm using Anaconda with Python 3.4.3-0.
I have installed using conda install the news version of pandas (0.17.0), seaborn (0.6.0), numpy (1.10.1), matplotlib (1.4.3), ipython (4.0.1)
Inside the nesarc_pds.csv I have this:
IDNUM,S1Q2I
39191,1
39787,1
40082,1
40189,1
40226,1
40637,1
41306,1
41627,1
41710,1
42113,1
42120,1
42720,1
42909,1
43092,1
7,2
15,2
25,2
40,2
46,2
49,2
57,2
63,2
68,2
100,2
104,2
116,2
125,2
136,2
137,2
145,2
168,2
3787,9
6554,9
7616,9
11686,9
12431,9
14889,9
17694,9
19440,9
20141,9
21540,9
22476,9
24207,9
25762,9
29045,9
29731,9
So, that being said, this is my code:
import pandas as pd
import numpy
import seaborn as snb
import matplotlib.pyplot as plt
data = pd.read_csv("nesarc_pds.csv", low_memory=False)
#converting variable to numeric
pd.to_numeric(data["S1Q2I"], errors='coerce')
#setting a new dataset...
sub1=data[(data["S1Q2I"]==1) & (data["S3BQ1A5"]==1)]
sub2 = sub1.copy()
#setting the missing data 9 = unknown into NaN
sub2["S1Q2I"] = sub2["S1Q2I"].replace(9, numpy.nan)
#setting date to categorical type
sub2["S1Q2I"] = sub2["S1Q2I"].astype('category')
#plotting
snb.countplot(x="S1Q2I", data=sub2)
plt.xlabel("blablabla")
plt.title("lalala")
And then.....this is the error:
Traceback (most recent call last):
File "C:/Users/LPForGE_1/PycharmProjects/guido/haha.py", line 49, in <module>
snb.countplot(x="S1Q2I", data=sub2)
File "C:\Anaconda3\lib\site-packages\seaborn\categorical.py", line 2544, in countplot
errcolor)
File "C:\Anaconda3\lib\site-packages\seaborn\categorical.py", line 1263, in __init__
self.establish_colors(color, palette, saturation)
File "C:\Anaconda3\lib\site-packages\seaborn\categorical.py", line 300, in establish_colors
l = min(light_vals) * .6
ValueError: min() arg is an empty sequence
Any help would be really nice. I pretty much exhausted my intelligence trying to understand how to solve this.
I tried to do a plot in the ipython notebook by calling a function that does the plot, but get an error. This is the code in the notebook:
from mynamd.logutils import plot_energy_column
plot_energy_column('3x3x3/eq0.log', 4) # print dihedral
This is plot_energy_column:
def plot_energy_column(logfile,column):
import matplotlib.pyplot as plt
plt.xlabel('time'); plt.ylabel(energy_keys[column])
plt.plot( fetch_energy_column(logfile,1), fetch_energy_column(logfile,column) )
plt.show()
Here's the error:
NameError Traceback (most recent call last)
<ipython-input-97-8ef7f9adebf3> in <module>()
1 from mynamd.logutils import plot_energy_column,fetch_energy_column
----> 2 plot_energy_column('3x3x3/eq0.log', 4) # print dihedral
3 #show()
/home/jbq/code/python/mynamd/logutils.py in plot_energy_column(logfile, column)
28 plt.xlabel('time'); plt.ylabel(energy_keys[column])
29 plt.plot( fetch_energy_column(logfile,1), fetch_energy_column(logfile,column))
---> 30 plt.show()
31
32
NameError: global name 'show' is not defined
I get the seme error if I remove plt.show() from the plot_energy_function definition and stick a show() at the end of the ipython notebook. Any clarifications are T-appreciated.
Jose