Iplot Scatter

  • A three-dimensional (3D) scatter plot is like a scatter plot, but with three variables — x, y, and z or f (x, y) are real numbers. The graph can be represented as dots in a.
  • Import pandas as pd. Import numpy as np.% matplotlib inline. From plotly import.
  • From chartstudio.plotly import plot, iplot. If you want to render the images locally, you have multiple options: from plotly.offline import iplot # your code iplot(fig). From plotly.subplots import makesubplots fig = makesubplots(# your args) # your code fig.show. Import plotly.io as pio # your code pio.show(fig).
  • Iplot # Click on legend to draw focus on specific element eg A & C. Scatter Plot¶ In 30: df. Iplot (kind = ‘scatter’, x = ‘A’, y = ‘B’, mode = ‘markers’, size.

Installing Required Libraries

To install the Plotly library using the ‘pip’ utility, you need to execute the following command:

Importing Required Libraries

Plotly is basically an online library that hosts your data visualizations, however, it also provides an offline data package that can be used to draw interactive plots offline.

Plotly for Basic Plots

In this section, we will be using the Plotly library to draw basic interactive plots. In the next section, we will see how Plotly can be used to plot geographical data.

The Dataset

The dataset that we are going to use for this section is the ‘Tips’ dataset that is downloaded by default with the Seaborn library. The dataset contains information about the amount spent by a group of people at lunch and dinner. The dataset contains gender, price, tips, age, size, day, time and whether the people who had lunch or dinner were smokers or not.

The Bar Plot

To plot the interactive bar plot using Plotly, you can use the iplot() function. You need to pass 'bar' as the value for the kind parameter of the iplot() function. Furthermore, you need to pass the list of the categorical columns for which you want to plot your graphs to the x attribute. Finally, the numerical column is passed as a value to the y attribute. The following script plots a bar plot for the time and sex columns on the x-axis and total_bill on the y-axis.

The Scatter Plot

To plot an interactive scatter plot, you need to pass ‘scatter’ as the value for the kind parameter of the iplot() function. Furthermore, you need to pass column names for the x and y-axis. The following script plots a scatter plot for the total_bill column on the x-axis and tip column in the y-axis.

The Box Plot

In one of my earlier articles I explained what is a box plot and how we can draw it using the Seaborn library. The box plot plots the quartile information for the numerical columns. The distance between the bottom whisker and the bottom of the box displays the first quartile. The distance between the bottom of the box to the middle of the box displays the second quartile. Similarly, the distance from the middle of the box to the upper end of the box quantifies the third quartile while the distance from the top of the box to the top whisker displays the last quartile.

The Hist Plot

The Plotly library can also be used to plot interactive histogram plots for any column in the dataset. To do so, you have to pass ‘hist’ as value to the kind parameter of the iplot() function. You can also specify the number of bins using the bins attribute. The following script plots histogram for the total_bill column:

The Scatter Matrix Plot

The scatter matrix plot is basically a set of all the scatter plots for numeric columns in your dataset.

The Spread Plot

The spread plot shows the spread between two or more than numerical columns at any particular point. For instance, to see the spread between total_bil and tip, you can use the spread function as follows:

Pandas plot scatter

3D Plots

Finally, in addition to 2D plots, you can also create 3-D interactive plots using Plotly library. For instance to see 3D plot for total_bill, tip and size columns, execute the following script.

Scatter

Plotly for Geographical Plots

To draw geographical plots with Plotly, we will use Choropleth Maps. Choropleth Maps are special types of Plotly plots that are used to plot geographical data. The detailed documentation regarding how to use the choropleth maps is available here.

Geographical Maps for the United States

There are four steps to drawing geographical maps using the Plotly.

  1. type: Since we are using choropleth maps, the type will always be choropleth.
  2. locations: Here we need to pass the abbreviations for the states that we want to display on our map. Four states will be displayed on our map: 'Michigan (MI)', 'Colorado (CO)', 'Florida (FL), 'Indiana (IN)'
  3. locationmode will be USA-state since we are only displaying the map for the United States.
  4. colorscale: This key is used to specify the color of the plot. Check the documentation for more color options.
  5. text: Contains a list of strings that will be displayed when the mouse hovers over the state location.
  6. The z key contains a list of numerical values that will be displayed when the mouse hovers over the state location.
  7. colorbar is a dictionary. For the title key, you can specify the text that will be displayed on the color bar.
Iplot Scatter

Geographical Maps for the United States Using CSV

Now we have a basic idea of how we can create geographical plots using Plotly and choropleth maps. Let us now create a more complex map. We will see how we can use data from a CSV file to create a geographical plot. We will create a geographical map that will display the Per Capita GDP for all the states in the United States.

Scatter Plot Iplot

The next step is to add a column in the dataset that contains abbreviations. We can do so by mapping the values in the Area column to the keys in the us_state_abbrev dictionary. The corresponding values can then be added to the newly created abbreviation column as shown below:

Pandas Plot Scatter

The next step is to create the layout for our map. The following script does that:

Iplot

Geographical Maps for the World

In the previous sections, we saw graphical maps for the United States. In this section, we will see how to plot geographical maps for the world. The process remains more or less similar. As a first step, we will create a data dictionary, followed by the layout dictionary and the graph object. Finally, we will use the iplot() function to plot the graph.

Conclusion

Scatter

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store