Iplot Scatter

Posted By admin On 07/04/22

Scatter Plot Scatter Plot Matplotlib Scatter Plot in Python Matplotlib scatterplot. Matplot has a built-in function to create scatterplots called scatter. Scatter(x,y,sz,c) specifies the circle colors.To plot all circles with the same color, specify c as a color name or an RGB triplet. To use varying color, specify c as.

Python is great for data exploration and data analysis and it’s all thanks to the support of amazing libraries like numpy, pandas, matplotlib, and many others. During our data exploration and data analysis phase it’s very important to understand the data we are dealing with, and for that visual representations of our data can be extremely important.

It is common for us to work on these projects using Jupyter notebooks because they are great, fast, simple and they allow us to interact and play with our data. However there are limitations to what we can do, normally when we work with charts we use libraries like matplotlib, or seaborn, but those libraries render static images of our charts and graphs. But many things get lost in the details, and thus we need to fine-tune our charts just to explore sections of our data. Wouldn’t it be great if we could just interact with our charts by zooming in, adding contextual information to our data points like hover interactions? Here is where Plotly can help us.

Plotly is a python library that makes interactive, publication-quality graphs like line plots, scatter plots, area plots, bar charts, error bars, box plots, histograms, heatmaps, subplots, and much much more.

But we talked enough, let’s start building some charts…

Installing Dependencies

Before we build anything, let’s install dependencies. I like to use pipenv but the same applies to anaconda or other package managers.

Here is the list of dependencies we need

  • jupyter: Web application that allows you to create and share documents that contain live code, equations…. you know it!
  • pandas: Very powerful library for data analysis in general and we will use it in our project to handle our data
  • numpy: Scientific computing for Python, used in our project for math and generating random numbers
  • seaborn: Statistical data visualization based on matplotlib, we will be using it to load some sample data that comes with the library
  • cufflinks: Allows plotly to work with pandas
  • plotly: Interactive charting library

Here are the commands to install them:

Getting Started

To get started we need to start our jupyter notebook and create a new document:

Plot

Once we are there we can start adding some code. Since this article is not a tutorial on Jupyter Notebooks, I’ll just focus on the code and not on how to use the document.

Let’s start importing the libraries:

Plotly with the help of other libraries can render the plots in different contexts, for example on a jupyter notebook, online at the plotly dashboard, etc. By default, the library works with the offline mode, which is what we want. However, we also need to tell cufflinks that we will be using the offline mode for the charts. This setting can be done programmatically by adding the following cell to our notebook:

Now we are ready to get some data and start plotting.

Generating Random Data

I don’t want to focus so much on how to load or retrieve data, so for that reason, we will simply generate random data for the charts, in a new cell we can use pandas and numpy to build a 3d matrix:

Awesome, using numpy we can generate our random numbers and we can load them into a pandas DataFrame object. Let’s see what our data looks like:

and we get:

That’s great! time to plot some charts.

Our first plots

A convenient way to plot DataFrames is by using the method iplot available on Series and DataFrames, courtesy of cufflinks. Let’s start with all the defaults:

At simple sight, it looks like any other chart, but if you hover with your mouse over the chart you will start seeing some magic. A toolbar appears on hover on the top right of the screen that allows you to zoom, pan, and other things. The chart also allows you to zoom in by drawing an area on the chart or to simply see a tooltip on each data point with additional information like the value.

Scatter

Our chart above is certainly better than a static chart, however is still not great. Let’s try to render the same chart using a scatter plot.

Not terrible, but not great, the dots are too big, let’s resize them:

Iplot Scatter

Much better! next, let’s try something different.

Bar Charts

Let’s forget our randomly generated dataset for a minute, and let’s load a popular dataset from the seaborn library to render some other chart types.

The dataset we will be working on is called “titanic”, and contains information about what happened to the people who were traveling on the titanic that tragic day.

One special variable in this dataset is the survived variable, which contains boolean information, 0 for those who died, and 1 for those who survived the accident. Let’s build a bar chart to see how may man and woman survived:

The trend can be easily seen, however, if you just share this chart it’s impossible to know what we are talking about as it has no legends, nor titles. So let’s fix that:

That’s now much better!

Cufflinks iplot scatterPandas

But what if we want to draw a horizontal bar plot? Easy enough:

Great! Let’s explore some more functionality

Themes

Our charts are so far looking great, but perhaps we want to use a different color schema for our charts. Luckily enough, we have a set of themes that we can use to render our plots. Let’s list them and switch to another one.

Listing themes:

It should output something as follows:

We can switch the theme for all future charts by simply adding:

And now if we render our bar chart again we get something like:

Dark mode, one of my favorites, but please check them out and let me know which one is your favorite.

Surface Charts

So far we rendered amazing 2d charts, but plotly also supports 3d charts. Let’s build some 3d charts to have some fun.The next plot that we will make it the 3D Surface plot and for that, we need to create some data using pandas as you see in the following:

Pandas Plot Scatter

You should get something like:

Now let’s throw this on a 3d chart using the “surface” kind.

Looks amazing! and colorful, let’s change the color scale to make it more visually appealing:

Beautiful! But that’s not it, have you tried interacting with the chart in your notebook? You can even rotate it!

Conclusion

Plotly is a great chart alternative for your data exploration and analysis. As seen it provides interactive dashboards that can help you identify better your outliers and get a better understanding of your data by navigating through it.I probably won’t use plotly for every single dataset, but it’s a very interesting library that we should know about.

Iplot Scatter

Thanks for reading!