Preparing for Pandas and Plotly:

  • In our Data Visualization Unit, we’ll be using two Python libraries: pandas (for working with tabular data) and Plotly (for building interactive charts).
  • To get ready, install both libraries on your computer before we start, then run the check below to confirm they work.

Step 1: Install the Libraries:

  • Run the command that fits your computer:
    • Windows: run pip install pandas plotly in the terminal.
    • Mac: run pip3 install pandas plotly in the terminal. Use pip3, not pip.

Step 2: Check the Installation:

  • Run the code below. If everything is set up, you’ll see the version numbers and a success message.
  • Save the code in a file named pandasPlotlyCheck.py and run it in the same way you’ve run your other exercises.
    • Don’t name the file pandas.py or plotly.py. Python would import your file instead of the library and the check would fail.
python
1"""
2Run this file to check that pandas and plotly are installed correctly.
3
4If everything works, you'll just see version numbers and a success
5message printed below — no files are created.
6"""
7
8import sys
9
10try:
11    import pandas as pd
12    import plotly
13    import plotly.offline as offline
14    import plotly.graph_objs as go
15except ImportError as e:
16    print(f"Import failed: {e}")
17    print("pandas and/or plotly are not installed correctly.")
18    print("Try: pip install pandas plotly   (Mac: pip3 install pandas plotly)")
19    sys.exit(1)
20
21print(f"pandas version: {pd.__version__}")
22print(f"plotly version: {plotly.__version__}")
23
24# Build a tiny dataframe and a tiny plotly figure, then render it to an
25# HTML string in memory (nothing written to disk). This confirms the
26# two libraries actually work together, not just that they import cleanly.
27df = pd.DataFrame({"x": [1, 2, 3, 4], "y": [4, 1, 6, 3]})
28trace = go.Scatter(x=df["x"], y=df["y"], mode="lines", name="sample data")
29fig = go.Figure(data=[trace])
30
31html_snippet = offline.plot(fig, output_type="div", include_plotlyjs=False)
32
33if html_snippet and "<div" in html_snippet:
34    print("\nSuccess! pandas and plotly are working together.")
35else:
36    print("\nSomething's off — the plot didn't render as expected.")

Still Seeing an Error?

  • If you see an import error, double-check that you ran the install command in Step 1, then run the code again.
  • If it still isn’t working, uninstall your current version of Python, then reinstall it by following the steps in Python Installation.
    • Windows users: during the installation, make sure to check the Add python.exe to PATH checkbox.