data-8 / datascience

A Python library for introductory data science
https://www.data8.org/datascience/
BSD 3-Clause "New" or "Revised" License
626 stars 295 forks source link

plots don't work with datascience module #606

Closed avanitanna closed 11 months ago

avanitanna commented 11 months ago

I followed the documentation (for example, https://www.data8.org/datascience/_autosummary/datascience.tables.Table.scatter.html) and got the following error:

>>> table = Table().with_columns(
... 
...     'x', make_array(9, 3, 3, 1),
... 
...     'y', make_array(1, 2, 2, 10),
... 
...     'z', make_array(3, 4, 5, 6))
>>> 
>>> table
x    | y    | z
9    | 1    | 3
3    | 2    | 4
3    | 2    | 5
1    | 10   | 6
>>> table.scatter('x')
# /opt/conda/lib/python3.11/site-packages/matplotlib/backends/__pycache__/__init__.cpython-311.pyc matches /opt/conda/lib/python3.11/site-packages/matplotlib/backends/__init__.py
# code object from '/opt/conda/lib/python3.11/site-packages/matplotlib/backends/__pycache__/__init__.cpython-311.pyc'
import 'matplotlib.backends' # <_frozen_importlib_external.SourceFileLoader object at 0x7fea08336b10>
# trying /opt/conda/lib/python3.11/site-packages/matplotlib/backends/backend_agg.cpython-311-x86_64-linux-gnu.so
# trying /opt/conda/lib/python3.11/site-packages/matplotlib/backends/backend_agg.abi3.so
# trying /opt/conda/lib/python3.11/site-packages/matplotlib/backends/backend_agg.so
# trying /opt/conda/lib/python3.11/site-packages/matplotlib/backends/backend_agg.py
# /opt/conda/lib/python3.11/site-packages/matplotlib/backends/__pycache__/backend_agg.cpython-311.pyc matches /opt/conda/lib/python3.11/site-packages/matplotlib/backends/backend_agg.py
# code object from '/opt/conda/lib/python3.11/site-packages/matplotlib/backends/__pycache__/backend_agg.cpython-311.pyc'
# trying /opt/conda/lib/python3.11/site-packages/matplotlib/backends/_backend_agg.cpython-311-x86_64-linux-gnu.so
# extension module 'matplotlib.backends._backend_agg' loaded from '/opt/conda/lib/python3.11/site-packages/matplotlib/backends/_backend_agg.cpython-311-x86_64-linux-gnu.so'
# extension module 'matplotlib.backends._backend_agg' executed from '/opt/conda/lib/python3.11/site-packages/matplotlib/backends/_backend_agg.cpython-311-x86_64-linux-gnu.so'
import 'matplotlib.backends._backend_agg' # <_frozen_importlib_external.ExtensionFileLoader object at 0x7fe524e14410>
import 'matplotlib.backends.backend_agg' # <_frozen_importlib_external.SourceFileLoader object at 0x7fe524e0f210>

It seems like there's an issue with the way datascience communicates with matplotlib. Also, please find a screenshot attached showing that matplotlib works and then after an import statement (from datascience import *), matplotlib stops working. bug_report_datascience

avanitanna commented 11 months ago

Workaround - use these imports to see the plots

%matplotlib inline
import matplotlib.pyplot as plots

from IPython.display import display, Image
adnanhemani commented 11 months ago

Not sure if you need to have the IPython import, but you will require the %matplotlib inline or similar magic command to ensure that matplotlib knows which mode to display the plot in.

Here's the import block we use in our test cases and Data 8 curriculum:

from datascience import *
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')

I'm closing this ticket out as this is known and expected for usage. Please feel free to re-open if you think there is something else that is being messed up in your environment altogether.

Nasir-Hussain-Datascientist commented 10 months ago

include "%matplotlib inline" in your code after importing libraries, %matplotlib inline is a command in Jupyter Notebooks that ensures Matplotlib-generated plots are displayed directly within the notebook interface.