Bokeh doesn't find Bar-chart modules on Raspberry Pi - python

_I'm trying to make bar-charts with Bokeh to be outputted as .html-files. Everything works fine with simple plotting, but for some reason when i try to run the following code:
from bokeh.charts import Bar, output_file, show
from bokeh.sampledata.autompg import autompg as df
p = Bar(df, label='yr', values='mpg', agg='mean',
title="Average MPG by YR")
output_file("bar.html")_
I end up with an error saying:
ImportError: No module named 'bokeh.charts'
I have installed Pandas, Numpy through pip and they all can be found using the help('modules') command. I've understood that Pandas is required for high-level Bokeh charts and it's been installed through Pip
Pandas version: 0.20.3
Bokeh version: 0.12.9
Python is version 3.4.2
I've tried also different import commands, "from bokeh import *" etc. but nothing seems to work.

In addition to Holoviews, if you just need simple bar charts, these are also now easy to make from the stable bokeh.plotting API, e.g.:
group = df.groupby('cyl')
source = ColumnDataSource(group)
cyl_cmap = factor_cmap('cyl', palette=Spectral5, factors=sorted(df.cyl.unique()))
p = figure(plot_height=350, x_range=group, title="MPG by # Cylinders")
p.vbar(x='cyl', top='mpg_mean', width=1, source=source,
line_color=cyl_cmap, fill_color=cyl_cmap)

bokeh.charts has been deprecated. You can get back the functionality by installing and importing the bkcharts package but this won’t be supported in the future. Bokeh devs recommend checking holoviews (which use bokeh as a possible back-end)

Related

The plotly.grid_objs module is deprecated for jupyter notebook offline plot

I am following a tutorials in plotly official website. I am using jupyter notebook. and I got an import error for plot.grid_objs. I am using plotly 4.0. Any suggestion to fix it? Thanks
https://plotly.com/python/v3/gapminder-example/
ImportError:
The plotly.grid_objs module is deprecated,
please install the chart-studio package and use the
chart_studio.grid_objs module instead
import plotly as py
import plotly.figure_factory as FF
from plotly.grid_objs import Grid, Column
import pandas as pd
import time
import pickle
filename_pickle='dataset.pkl'
try:
dataset=pd.read_pickle(filename_pickle)
except FileNotFoundError:
url = 'https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv'
dataset = pd.read_csv(url)
dataset.to_pickle(filename_pickle)
table = FF.create_table(dataset.head(10))
py.offline.iplot(table) # table is inline inside jupyter notebook
ImportError:
The plotly.grid_objs module is deprecated,
please install the chart-studio package and use the
chart_studio.grid_objs module instead
Looks like the tutorial is for a much older version of plotly. Try:
pip install plotly==2.0.1
in your terminal and then rerun the Python code.
By referencing the documentation, you can find that do something as seen here:
import chart_studio
from chart_studio.grid_objs import Column, Grid
It also says to Replace plotly.grid_objs with chart_studio.grid_objs in the version 4 migration guide. In this case, a version downgrade is not necessary.

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

how to copy matplotlib figures to clipboard

After plotting the figure, I want to copy it to the clipbaoard instead of saving to figures. I searched the internet and the code does not work, either the required modules only support python 2.x or the program stopped by not well-defined functions.
I am using Python 3.6 in Windows 10. My matplotlib version is 2.0.2.
I tried 'PyGTK: copy matplotlib figure to clipboard'
and got the error: ImportError: Gtk* backend requires pygtk to be installed. I then visited www.pygtk.org/downloads.html to download pygtk/PyGObject (Windows) but it requires python 2.6 or file not found.
Can anyone show how to achieve it?
import matplotlib.pyplot as plt
a = [2,3,6,7,1]
plt.plot(a)
plt.show()

Matplotlib Basemap Equivalent Python 3

Non Code general question: I am running Python 3.5.2 (Anaconda distribution) and new to Python. I want to create a heat map over layed onto map of USA. All searches I've done say use 'basemap' which seems to be unsupported in Python 3. What alternatives are out there (do not want to revert to previous Python).
You can use Seaborn python module it works for python3 as well.
And here is the help regarding heatmap using seaborn.
Or you may use Geoplot python module
geoplot is another library that allows you to create choropleth maps. These can then be overlayed on a webmap using mplleaflet.
Basemap is supported in python 3. With the anaconda install of python 3.7 I used conda install basemap. At this point you might need to set you PROJ_LIB if you are running in the base environment, as shown in the following example script:
import os
os.environ['PROJ_LIB'] = r'C:\Users\YOURUSERNAME\Anaconda3\pkgs\proj4-5.2.0-ha925a31_1\Library\share'
Now you should be able to use basemap in python 3. Here's an example I adapted from Dan Nguyen's github (I removed the Earthquake data and just left the map itself, see the link for neat stuff):
# %matplotlib # in iPython
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
plt.figure(figsize=(14, 8))
earth = Basemap()
earth.bluemarble(alpha=0.42)
earth.drawcoastlines(color='#555566', linewidth=1)
plt.show()
Resultant image:

Matplotlib stopped working after upgrade to 1.1.0

I'm trying to run matplotlib 1.1.0 under Ubuntu 11.10. I previously had matplotlib 1.0.1 installed and working, but I needed some features from version 1.1.0 (and was becoming quite annoyed that all the documentation was for another version than mine) so I decided to upgrade.
Now, I can't show any plots.
I have uninstalled everything I had from the package repository, and installed version 1.1.0 from source. I've read through the installation FAQ but the hints there did not help. I get the following output from their suggested troubleshooting procedure, but no plot:
$HOME=/home/tomas
CONFIGDIR=/home/tomas/.matplotlib
matplotlib data path /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data
loaded rc file /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc
matplotlib version 1.1.0
verbose.level helpful
interactive is False
platform is linux2
Using fontManager instance from /home/tomas/.matplotlib/fontList.cache
backend agg version v2.2
What do I need to do to get this working?
Update:
After following up on some troubleshooting tips in comments, I can report that it's the actual showing of plots that doesn't work.
Running the following script outputs a png with the expected plot, but does not show any plotting window.
from matplotlib import pyplot as plt
plt.plot([1, 2, 3])
plt.savefig('testfig.png')
plt.show()
When you import pyplot, it has to set a backend, and it may be setting it to a non-interactive one, which would explain the behavior you are seeing. See what backend is being used by running plt.get_backend()
To set the backend, you have to run these commands before importing pyplot:
import matplotlib
matplotlib.use(your_backend)
where
your_backend in set(["FLTKAgg", "GTK", "GTKAgg", "GTKCairo", "macosx",
"QTAgg", "QT4Agg", "TkAgg", "WX", "WXAgg"])
To make this a permanent setting, put the backend name (without quotes) in your ~/.matplotlib/matplotlibrc file, like this example:
backend : WXAgg

Categories