how to show boxplot in jupyter? - python

I have problem with showing below plot
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.boxplot(x=tips["total_bill"])`enter code here`
The error is: module 'seaborn' has no attribute 'set_theme'
What should I do?

I got the same problem, then I read the release note of v0.11.0 (September 2020) https://seaborn.pydata.org/whatsnew.html
Theming
API The set() function has been renamed to set_theme() for more clarity about what it does. For the foreseeable future, set() will remain as an alias, but it is recommended to update your code.
So I uninstall and reinstall the seaborn, now it works :)
pip uninstall seaborn
pip install seaborn

Related

How can address this python plotly.express error: "Mime type rendering requires nbformat>=4.2.0 but it is not installed"

I faced a problem in python and was wondering if someone is able to help me solve it.
I get below error when I try to use the the "show()" function for “plotly.express” library.
ValueError: Mime type rendering requires nbformat>=4.2.0 but it is not
installed
I tried below 2 methods to solve, it still does not work. They are the recommended solutions by many sources online.
1 - pip install ipykernel
2 - pip install --upgrade nbformat
Any idea how to fix it?
Thank you,
My code:
import pandas as pd
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
data = pd.read_csv('daimond_data.csv')
data = data.drop('Unnamed: 0', axis=1)
fig = px.scatter(data_frame=data, x='carat', y='price', size='depth',color='cut', trendline='ols' )
print(fig.show())

How do I import data and packages from REST APIs

I'm currently trying to learn Python through coursera and I'm trying to run the code but I'm running into an error when I try to install nba_api. Below I have pasted my code
!pip install nba_api
# I get an 'invalid syntax error' here ^^
import pandas as pd
import matplotlib.pyplot as plt
x={'a':[11,21,31],'b':[12,22,32]}
print(x)
df=pd.DataFrame(x)
print(type(df))
print(df.head())
from nba_api.stats.static import teams
import matplotlib.pyplot as plt
Any ideas what I'm doing wrong/missing here?
! is a shorthand for calling out to the shell that is only in the REPL. You can't use this in Python files.
Instead you should put your code in a package and have a dependency on nba_api, when someone installs your package, nba_api package would also be installed.

Why Plotly is not working on Jupyter Lab?

I had to update my Macbook Air's iOS (Catalina), since I updated to this iOS's version, plotly stop working on my Jupyter-lab's enviroment.
I already followed the Getting Started's steps showed on plotly website:
$ pip install jupyterlab==1.2 "ipywidgets>=7.5"
When I run a random example, it shows an empty white block:
The libraries that I was importing (The ones that worked before):
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import cufflinks as cf
from plotly.offline import download_plotlyjs,init_notebook_mode, plot,iplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)
cf.go_offline()
%matplotlib inline
You need to have the relevant JupyterLab extensions installed, as per our Getting Started guide here: https://plot.ly/python/getting-started/
If you're having trouble, here is our troubleshooting guide: https://plot.ly/python/troubleshooting/

cannot plot with matplotlib and pandas

I am using OSX (Mojave 10.14.3) and am having a strange issue plotting a pandas (0.24.2) dataframe using matplotlib (3.0.3). I am using python 3.7.3.
So, the code is as:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'name':['john','mary','peter','jeff','bill','lisa','jose'],
'age':[23,78,22,19,45,33,20],
'gender':['M','F','M','M','M','F','M'],
'state':['california','dc','california','dc','california','texas','texas'],
'num_children':[2,0,0,3,2,1,4],
'num_pets':[5,1,0,5,2,2,3]
})
df.plot(kind='scatter',x='num_children',y='num_pets',color='red')
plt.show()
All this does is show an empty window with nothing in it. I was expecting a scatterplot with 7 points. The example is taken from the web tutorial as is.
EDIT
plt.savefig('myfilename.png')
Savefig works.
I am not sure if this will help anyone but I basically had to install python as a framework to make it work. I was using Anaconda, so something like:
conda install python.app
pythonw script.py # note using pythonw
I, then, was able to get the plot to render correctly by using the macosx backend:
import matplotlib as mpl
mpl.use('MacOSX')

Jupyter Notebook - Matplotlib keep running

I just started to use Jupiter Notebook to learn Python. while I am trying out matplotlib with this basic code:
import numpy as np
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
The kernel just keep running and nothing happen. How to resolve this? Is there an dependency issue? My newly installed matplotlib is 1.5.1, python is 3.5.2, numpy is 1.11. Running on MacBook Pro 10.11(El Capitan).
To Visualize the plots created by the matplotlib in Jupiter Notebook or ipython notebook you have add one extra line at the beginning.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
If your matplotlib version is above 1.4, and you are using IPython 3.x you have to use the below code.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
It likely showed you a Matplotlib popup, with the IP(y) logo. To show it inline you have to use some IPython magic. Run this in a cell somewhere:
%matplotlib inline
After you close the popup it will finish the statement in your kernel
It sometimes takes time until the kernel starts.
Check that the code is color-highlighted. If it is, it means that the kernel is running. Evaluate the cell again. You will notice a * beside that cell, meaning it's running.
And one more thing: Sometimes the plot is displayed but its window hides behind the notebook... Access it from the task bar

Categories