Creating Scatter and Line Charts with Python Plotly: A Step-by-Step Tutorial
This article describes how the author overcame Jupyter setup issues and used Python with Plotly to generate scatter and line chart examples, providing the full source code and screenshots of the final visualizations.
I spent two hours troubleshooting while learning Python and Plotly for data processing, eventually completing examples of a scatter plot and a line chart.
Because the official example uses online storage, it requires Jupyter (iPython) notebooks to handle generated files; initially I confused iplot with plot, which caused wasted time.
Important note: the latest Jupyter does not support Python 3.2 and earlier versions.
To work around the issue I switched to using local files. Below is the test code I used (the official code and offline storage code are commented out). This solution is suitable for the newest Python versions.
#!/usr/bin/python
# coding=utf-8
import plotly.plotly
import random
from plotly.graph_objs import *
import plotly.graph_objs as abc # 必须
import numpy as np
def sayHello():
N=100
xx = []
for i in range(20):
xx.append(i)
y0 = []
for i in range(20):
y0.append(random.randint(0, 10))
y1 = []
for i in range(20):
y1.append(random.randint(10, 20))
y2 = []
for i in range(20):
y2.append(random.randint(20, 30))
data_1 = abc.Scatter(
x=xx,
y=y0,
name='test1',
mode='markers'
)
date_2 = abc.Scatter(
x=xx,
y=y1,
name='test2',
mode="lines"
)
date_3 = abc.Scatter(
x=xx,
y=y2,
name='test3',
mode="lines+markers"
)
data1 = Data([data_1, date_2, date_3])
plotly.offline.plot(data1)
#plotly.offline.iplot(data1,filename='test01')
if __name__ == "__main__":
sayHello()The final result is shown in the screenshot below.
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.