I am using Jupyter Lab 2.0, matplotlib 3.2.0 and Python 3.8.0
So, I found out you can get interactive plots in Jupyter Lab, and embarked on the journey to make this happen for myself. Along the way, here are the things I tried:
Ensured nodejs is installed
Installed the Jupyter Lab extension manager
Installed ipympl
Installed jupyter-matplotlib
Re-build JupyterLab
Restart JupyterLab (several times)
Next, in my code I tried all of the following common recommendations (alone and in various combinations):
matplotlib.use("nbragg")
%matplotlib ipympl
%matplotlib widget
%matplotlib inline
At best, some of these cause all of my rcParams defaults to be overridden, and show tiny graphs with no interactivity. At worst, my graphs don't show at all, except for sometimes displaying some random text below the code.
I have tried all of the suggestions from the following SO answers, GitHub and matplotlib docs:
jupyterlab interactive plot
https://github.com/matplotlib/ipympl/issues/9
https://github.com/matplotlib/ipympl/issues/133
https://matplotlib.org/users/prev_whats_new/whats_new_1.4.html#the-nbagg-backend
Sample of my working code:
train_df = pd.read_csv('data/train.csv', index_col='PassengerId')
test_df = pd.read_csv('data/test.csv', index_col='PassengerId')
survival = train_df.pop('Survived')
df = pd.concat([train_df, test_df])
# extract last names and titles:
df['Title'] = df.Name.str.extract(r'([A-Za-z]+)\.')
# extract last name:
df['LName'] = df.Name.str.extract(r'([A-Za-z]+),')
# Plot histogram of parsed titles:
sns.countplot(df.Title).set_title("Histogram of Passenger Titles")
plt.show()
Note that I have tried this with and without plt.show().
Related
I am trying to make my inline plots in jupyterlab interactive. So far, I have tried a suggestion as pointed out here, among others:
jupyterlab interactive plot
# %matplotlib notebook - does not work : Javascript Error: IPython is not defined
# %matplotlib widget - works, but plots are overwritten
The widget magic works in making the plots interactive, but unfortunately, my plots are overwritten. Subsequent cells render plots on top of the output of cell 1 as below:
plt.scatter(trainData['x'], trainData['y'], color='Red', s=0.5)
plt.show()
plt.hist(trainData['x'])
plt.xlabel("Values of X")
plt.ylabel("Frequency")
plt.show()
plt.hist(trainData['y'])
plt.xlabel("Values of Y")
plt.ylabel("Frequency")
This does not look right, have I missed an option? I am looking for an IPython magic that can help with having several inline plots (after each cell)?
I am using jupyterlab v 0.35.4 on chrome Version 74.0.3729.169, run from Anaconda navigator on windows 10.
I'm trying to make a Holoviews Sankey plot in Jupyter notebook (running Jupyter notebook using Pycharm), but the Sankey plot does not appear after the block is executed.
First I import required packages and set 'bokeh' as Holoviews extension. Using holoviews 1.10.9 and jupyterlab 0.35.4.
import pandas as pd
import holoviews as hv
hv.extension('bokeh')
After performing some data wrangling, I prepare a dataframe 'private_investments_grouped' with 3 columns (source, target, value).
I then execute the following code to make the Sankey plot for a sub-dataframe:
hv.Sankey(private_investments_grouped.loc[1:7]).options(label_position='left')
No plot appears in the output block, no pop-ups, no error messages. It appears the code executes, but no visualization appears.
I can use the 'bokeh' renderer in a Python file in Pycharm and store the Sankey plot as a html file, which works fine. However, I'd like to see the plot in Jupyter notebook.
I tried running plt.show() but no plot is shown. I had tried lots of solutions from stackoverflow including setting matplotlib backend to Qt4Agg, switching to other backends (i.e. TkAgg, Agg) and reinstalling matplotlib package but still not solving the issue. Some solutions that I had tried but not working are:
matplotlib does not show my drawings although I call pyplot.show()
Matplotlib.Pyplot does not show output; No Error
Why matplotlib does not plot?
Matplotlib does not show labels or numbers
Below is the code that I tried to run:
plt.scatter(pf["age"], pf["friend_count"])
plt.xlabel = "age"
plt.ylabel = "friends count"
plt.title = "Facebook Friends count by Age"
plt.show()
When plt.scatter(pf["age"], pf["friend_count"]) code was run, a scatter plot was shown but with no labels and title. Running plt.show() did not plot and no error was shown. Appreciate any help.
I installed Anaconda with Python 3 for Windows OS. My Mac laptop is running on Bootcamp.
The labels must use parentheses such as plt.xlabel("text"), not assigned to a string with = like you have in your example code. Make the changes in your code, save the changes, quit and reopen Spyder or whatever interpreter you are running, then run the code again.
plt.figure(1)
plt.scatter(pf["age"], pf["friend_count"])
plt.xlabel("age")
plt.ylabel("friends count")
plt.title("Facebook Friends count by Age")
plt.show()
I am creating a bar chart with seaborn, and it's not generating any sort of error, but nothing happens either.
This is the code I have:
import pandas
import numpy
import matplotlib.pyplot as plt
import seaborn
data = pandas.read_csv('fy15crime.csv', low_memory = False)
seaborn.countplot(x="primary_type", data=data)
plt.xlabel('crime')
plt.ylabel('amount')
seaborn.plt.show()
I added "seaborn.plt.show() in an effort to have it show up, but it isn't working still.
You should place this line somewhere in the top cell in Jupyter to enable inline plotting:
%matplotlib inline
It's simply plt.show() you were close. No need for seaborn
I was using PyCharm using a standard Python file and I had the best luck with the following:
Move code to a Jupyter notebook (which can you do inside of PyCharm by right clicking on the project and choosing new - Jupyter Notebook)
If running a chart that takes a lot of processing time it might not have been obvious before, but in Jupyter mode you can easily see when the cell has finished processing.
I'm using IPython notebooks to save my results and perhaps to share code including graphics. I am using ggplot right now. But I cannot get ggplot to plot inside the notebook output area. It always gives me a pop-up window that shows the plot. I don't know how to save it along with the notebook easily. Is there something I need to configure to make that happen? "%matplotlib inline" I saw in a ggplot tutorial that below code should do it. What am I missing?
My code:
plot = ggplot(my_dataframe, aes("x")) + geom_histogram()
print plot
I got my answer elsewhere. It worked like a charm!
%pylab inline