Introduction: Plotly With Python

Recently, I stumbled upon Plotly, a beautiful online Data Visualization system by virtue of a MAKE article. It is redundant for me to say that there are various articles which show the utilisation of Plotly for Arduino, Raspberry Pi, etcetera.

Unfortunately, there is no precise and simplified article for the utilisation of Plotly for Python.

Python, is an extremely simple programming language, that even the most ignoramus can learn in a week, and it would be extremely easy to incorporate Plotly into it.

Let me not digress.

Note: This Instructable is oriented around a Windows computer, with Python 2.7. Others can tag along as well, but may need to figure out certain aspects themselves.

Step 1: What Will I Need?

Worry not, this does not require too many materials.

In a nutshell, you will need.

1.) Python, which can be found online at Python, which can be downloaded for free here. .( I used this particular version, but it is entire up to you what YOU would like.) It is preferred that you already program with python, if not, learn the Basics and return. I will not be a spoilsport, and will keep a Beginner step for people starting off with Python.

2.) A Plotly account, which is free.

3.) A Computer or Laptop. Personally I used a Windows laptop, but Mac OS works as well.

4.) A terminal (eg. Command Prompt) { If you have no clue on how to change Directories in Command Prompt, read the Beginners step.}

5.) Something to graph.

Step 2: Beginners Ahoy!

So, never heard of Python before?

It is okay. After you download Python, add it to the Environment Variable PATH, using this route - Control Panel > System and Security > System > Advanced System Settings > Environment Variables > System Variables > Path.

In the End, add ;C:\Python27\ .

IDLE is the python GUI, and a code can be saved with a new window. (CNTRL + N)

Also, you can navigate in Command Prompt (or Cmd) with the statement cd for change directory. Usually a directory is a folder. To find out the directories available for navigation, type dir.

To go back, type cd .., and to change directory, type cd (Name of directory).

All set?

Step 3: Get Easy Install and Pip

Pip and Easy Install are used for installing packages, such as the Plotly package which can be installed with pip.

It is best to install pip with Easy_Install.

Open a new Window in Python, and enter the code from the attached file. Save the file in Python 27, Tools, Scripts, and run it.

You should see a screen like the one in the picture, and get a long list of words. Now, you have installed easy_install. Let us move on to pip.

Open Cmd, and navigate, using cd to C:\\ > Python27 > Scripts.

Next type in easy_install pip to install pip.

You can use pip to install various other modules as well, other than the one we shall install in the next step.

Step 4: Preparing Plotly

Let the show begin.

Go to the Scripts, where you installed pip in the previous step, using Cmd, and type in the following-

$pip install plotly

The dollar sign should NOT be typed in, it just displays that you have to type this in your terminal. ( Cmd, in my case)

You should get a screen like the one in the picture, and you are good to go.

Next head over to the Plotly Website, and create a new account, if you do not have one already.

Next, you need your API key. Go to this page, and get it. API Key.

For, every Python-Plotly Graph, you NEED to have your username and API key in the form of this line-

py = plotly.plotly("Username", "API-Key")

Obviously you have to replace Username with you username, and API key with your API key.

You also have to import the Plotly module in every project -

import plotly

Now, let us proceed to the first graph.

Step 5: Line Graph

Personally, I find this graph to be the easiest to make, and is perfect for beginners.

The code is short, and here it is-

import plotly

py = plotly.plotly('Username','APIKey')

a = (1, 2, 3, 4 ,5) b = (2, 3, 5, 6, 1)

py.plot(a,b)

If you look at the picture, you will notice, that the graph has almost nothing significant, just a mediocre line chart.

a is the x axis, and b is the y axis. Their values are given in the brackets. py.plot() creates a plot from the given data.

Let us move on to something less soporific and more ambitious.

Step 6: Bar Graph

Jack and John, according to me, are two of the most clichéd names ever, and so I will use them.

Two children, Jack and John, are brothers. Let us look at their food habits in the following Bar Graph.

The code for this graph, is relatively longer, and if you follow closely, hardly any explanation is required.

import plotly

py = plotly.plotly('Username','APIKey')

cats=['Sweets', 'Meat', 'Veggies', 'Liquids']

JO = { 'name': 'John', 'x': cats, 'y': [14, 7, 2, 5], 'type': 'bar', 'marker':{ 'color': 'rgb(255,0,0)' } }

JA = {'name': 'Jack', 'x': cats, 'y': [5, 11, 10, 9], 'type': 'bar', 'marker': {'color': 'rgb(0,0,255)'} }

omicron = { 'title': 'Portions', 'autosize': False, 'width': 800, 'height': 700 , 'yaxis': {'name': 'food eaten in g'}, 'xaxis': {'name': 'cats'}, 'barmode': 'group','bargroupgap': 0.5,}

py.plot([JO, JA], layout=omicron)

cats are the mutual categories, I thought giving it a variable would simplify matters. JO, are the attributes of John.

JA are the attributes of Jack. Name is the name of the bar, x, and y, are the axes, type is referring to the type of graph, marker refers to the colour of the bar in RGB form. (Red, Green, Blue).

Omicron shows the various attributes of the graph, such as the title, size, axis name, legend, distance between bars, etc.

The advantage of using python, is that you can mathematically calculate even complicated data using formulae, like this Wired Science Article.

Step 7: A Little Extra Never Hurts

So now we can perform simple tasks like creating bar and line graphs, but what about others, like an area graph.

Worry not, for Plotly has answers, in the form of their amazing gallery. Here is the Link.

Oh, and also, just for the sake of goodbye, here is a code for an area graph -

import plotly
py = plotly.plotly("Username","APIKey")

x = {'x': [1,2,3,4],'y': [4, 2, 1, 5],'fill': 'tozeroy', 'name':'x'}

y = {'x': [1,2,3,4],'y': [1,5,1,5.5],'fill': 'tonexty', 'name':'y'}

lop= {'width':500,'height':500,'showlegend':True,'autosize':False, }

response = py.plot([x, y], layout=lop)

Note that fill means that the graph is to be filled, as an area graph.

The instructable ends here, and I hope you appreciated my tutorial for Plotly-Python.

Also, please do vote if you liked the instructable!

Comments

author
rmuir_data (author)2016-10-05

Hey, great introductory article!

I really hope that more people start using Plotly to create interactive visualisations with Python. It's a really flexible and interesting tool.

I've made a few <a href="http://richard-muir.com/blog/blogposts/">Plotly tutorials</a> available on my website - these will hopefully expand upon some of the data visualisation skills you've introduced here.

I've put an example picture below; here's the <a href="http://richard-muir.com/blog/wp-content/uploads/iP...>Plotly python tutorial</a> for how to make it.

Hope this helps!

Rich.

GDP-growth-rate-and-public-debt-4-750x533.png
author
mba7 (author)2014-09-21

I made an equivalent tool in python that print real time data from ADXL345 accelerometer.

https://github.com/mba7/SerialPort-RealTime-Data-P...

may be it will be helpful for someone

author
Pure Carbon (author)2014-03-26

oh, I am definitely voting for this. You answered some major questions I had about codeing, why can't there be more beginner friendly instructables like this one, thank you, and if you don't mind voting for the instructables in the portable work station contest that would be great.

About This Instructable

26,161views

42favorites

License:

Bio: Simplicity is the ultimate sophistication.
More by adithyashok:Arduino Random Music GeneratorSpinning Earth - Beginners Unity Project Easy Designs - Turtle Graphics Python
Add instructable to: