PyViz: Having trouble displaying visualizations side by side - python

I'm following the PyViz tutorial found here: http://pyviz.org/tutorial/01_Workflow_Introduction.html, and I am running into an issue with displaying images side by side when using the exact code from the tutorial (in the New York vs Texas Measles example).
I should mention that I did not download the conda environment as outlined in the tutorial. I opened a new Jupyter Notebook, installed all the specified libraries(I hope), and downloaded the examples from the PyViz github page.
The libraries I downloaded/imported are the following:
import holoviews as hv
import bokeh
import matplotlib
import pandas as pd
import datashader
import dask
import geoviews
import numpy as np
import hvplot.pandas
import hvplot
import holoviews as hv
hv.extension('bokeh', 'matplotlib', width="100")
The part of the tutorial code that does not work is the following:
by_state["Texas"].relabel('Texas') + by_state["Florida"].relabel('Florida')
Both statements run on their own, but the plus sign does not appear to work. The error states:
TypeError: gridplot() got an unexpected keyword argument 'width'
The rest of the tutorial runs smoothly. Does anyone have any insight into what could be going wrong here?
Thank you!

Related

How do I change the backend in CoCalc to show a matplotlib scatter graph?

I have a program that is working fine locally, but I need to use it in CoCalc on the cloud to share with a group. Everything works fine until the end, when I want to create a scatter graph, in standard way. There are a bunch of other specs, but essentially, it's just
plt.scatter(x, y, color = 'blue')
plt.show()
Nothing tricky. But in Cocalc, it tells me "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure."
Here are the imports from the beginning of the file:
import nltk
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
I've searched for this error and tried the solutions I've found, such as importing tkinter. So, for instance, if I change the headers to
import nltk
import tkinter as tk
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
I get a different error: "Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running."
I've seen instructions about pip installations and such, but a) I'm not sure the installations would make everything work for my group, and b) CoCalc generally has most of the standard packages ready to go, so maybe that's not the problem?
Obviously, I'm not experienced with these issues, so help is much appreciated.
If you're using a Jupyter notebook in CoCalc, then the above will just work fine. Here's an example:
https://cocalc.com/wstein/support/matplotlib-scatter-in-jupyter
If you're writing all of your code as part of a Python program (so, e.g., .py files) and running this from a Terminal, CoCalc will also fully support Tk and this working! However, you have to start a new X11 desktop session in CoCalc by clicking +New --> "Linux Graphical X11 Desktop", then run your Python program from the terminal in the upper left. You'll see any windows that pop up in the main window off to the right.
See https://cocalc.com/wstein/support/scatter-matplotlib-x11

iplot is unrecognized in Jupyter Notebook

I'm trying to follow along with the following YouTube tutorial:
https://www.youtube.com/watch?v=D35m2CdMhVs
I set up the environment through Anaconda and am using Jupyter to code. I imported pandas and plotly for the code and yet the iplot function is not recognized.
The import statements in the code look like this:
import plotly
import plotly.figure_factory as ff
import pandas as pd
This is the specific error message I'm getting:
NameError: name 'iplot' is not defined
The tutorial is from 2018 and the author specifies the versions of some of the dependencies at the beginning of the tutorial. According to this answer, there has been a change in plotly between v3 and v4. According to the official web site 4.0 came in 2019.
Please check for compatibility between what is in the tutorial and what the version of the API you're using offers. Also quite often people mix Python 2.x with 3.x (especially if they polute the environment set by Anaconda and similar with direct calls to pip/pip3).

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

ipython notebooks sometimes not finding functions in file

EDIT: This is not solved in the suggested duplicate; reloading the module
after editing the file doesn't help.
I have a python file "/home/Misc/misc_def.py" collecting some functions that I'm using in several ipython notebooks. The first cell in each notebook is
import csv
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
%matplotlib inline
sns.set_style('white')
from sys import path
path.append('/home/Misc')
import misc_def
However, the strange thing is that sometimes this works (the notebook can find the functions in the file) and sometimes it doesn't. I'm using notebooks in different folders, but I think this shouldn't matter since it's all absolute paths. The errors I get are standard for not finding functions; e.g.
NameError: name 'get_overlap_data' is not defined
Is there something unstable about the way I do it above?

Categories