Tutorial: Installing and Using Plotly with Python 2.7 on macOS
This guide walks through installing Plotly via pip, setting up a Plotly account and credentials, configuring privacy options, and provides complete Python 2.7 code examples to generate and display interactive charts on macOS.
I discovered that official Plotly tutorials were outdated for Python 2.7 on macOS, so I wrote this step‑by‑step guide.
Installation: use pip (or sudo pip, or pip install --upgrade) to install the Plotly package.
$ pip install plotly
or
$ sudo pip install plotly
or
$ pip install plotly --upgradeCreate a Plotly account and set credentials in interactive mode:
import plotly
plotly.tools.set_credentials_file(username='DemoAccount', api_key='lr1c37zw81')The credentials are stored in ~/.plotly/.credentials . Example content is shown in the article.
If using Plotly online, configure privacy settings:
import plotly
plotly.tools.set_config_file(world_readable=False, sharing='private') import plotly
plotly.tools.set_config_file(plotly_domain='https://plotly.your-company.com', plotly_streaming_domain='stream-plotly.your-company.com')Sample script that generates random data and plots it:
#!/usr/bin/python
# coding=utf-8
import plotly.plotly
import random
from plotly.graph_objs import *
import plotly.graph_objs as abc # must import as abc
listx = [];
for i in range(20):
listx.append(i)
print listx
listxx = listx
listy = [];
for i in range(20):
listy.append(random.randint(12, 20))
print listy
listyy = [];
for i in range(20):
listyy.append(random.randint(12, 20))
print listy
data_1 = abc.Scatter(x=listx, y=listy)
date_2 = abc.Scatter(x=listxx, y=listyy)
data1 = Data([data_1, date_2])
plotly.offline.plot(data1)Official test code from Plotly documentation:
import plotly.plotly as py
from plotly.graph_objs import *
trace0 = Scatter(x=[1, 2, 3, 4], y=[10, 15, 13, 17])
trace1 = Scatter(x=[1, 2, 3, 4], y=[16, 5, 11, 9])
data = Data([trace0, trace1])
py.plot(data, filename='basic-line')Note: the import statement import plotly.graph_objs as abc is required for the script to work.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.