Updated On : Aug-05,2022 Time Investment : ~30 mins

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

Pandas is the most preferred library nowadays by the majority of data scientists worldwide for working (loading, manipulating, etc) with structured datasets (tables).

Besides data management and manipulation functionalities, it provides very convenient data visualization functionality. It let create charts from dataframe directly by simply calling plot() method on it. We can create easily create charts like scatter charts, bar charts, line charts, etc with just a line of code (by calling "plot()" with necessary parameters).

The pandas data visualization uses the matplotlib library behind the scene. All the plots generated by matplotlib are static hence charts generated by pandas dataframe's '.plot()' API will be static as well.

But this is an era of interactivity. Almost everything is interactive nowadays (charts, apps, dashboards, etc).

Python also have many data visualization libraries like Plotly, Bokeh, Holoviews, Bqplot, Altair, etc that can generate interactive charts. It can be very helpful if we can generate interactive charts directly from the pandas dataframe.

To our surprise, there is a Python library named "cufflinks" that is designed with this aim in mind. It let us generate interactive charts based on plotly directly from pandas dataframe with one line of code.

What Can You Learn From This Article?

As a part of this tutorial, we have explained how to use Python library "cufflinks" to create interactive data visualizations. Cufflinks is built on top of another data visualization library named Plotly. The main aim of Cufflinks is to simplify data visualization by providing same API as that of pandas dataframe function "plot()" but generating interactive charts using Plotly. It provides two methods with same API as pandas "plot()".

  1. "iplot()": This method provides the majority of parameters which are almost the same as that of plot() which will make it easier for someone having knowledge on plot() to get used to it.

    • Want to Know All Parameters of "iplot()" Function?
      • Try help(df.iplot) in Jupyter Notebook or IPython shell to know about all possible arguments of "iplot()" function. It has a lot of arguments because it is a generic function to create many charts.
  2. "figure()": It is almost same as iplot() with only difference being that it returns Plotly Figure object which we can customize further if we have good knowledge of Plotly.

We'll primarily concentrate on Cufflinks 'iplot()' method as a part of this tutorial to generate various interactive plotly charts from pandas dataframe. All datasets are loaded at the beginning as pandas dataframe.

Apart from simply plotting charts, we have also explained various theming and styling options to improve chart aesthetics (look & feel). The datasets used to create charts are toy datasets available from scikit-learn.

Below, we have listed essential sections of Tutorial to give an overview of the charts covered. Apart from simple chart types, various versions of charts (like stacked bar charts, exploding pie charts, etc) are also covered.

Important Sections Of Tutorial

  1. Scatter Plots
    • Scatter Chart with Regression Line Fitted
  2. Bar Charts
    • Simple Bar Chart
    • Horizontal Bar Chart
    • Side-by-Side/Grouped Bar Chart
    • Stacked Bar Chart
    • Figure of Multiple Bar Charts
  3. Line Charts
    • Line Chart with Secondary Y-Axis
  4. Area Charts
  5. Pie Charts
    • Exploding Pie Chart (Pie Chart with Wedge Sticking Out)
  6. Histograms
  7. Box Plots
  8. Heatmaps
  9. CandleStick & OHLC Charts
  10. Bubble Chart
  11. 3D Bubble Chart
  12. 3D Scatter Chart
  13. Spread Chart
  14. Ratio Chart

Map Charts using "cufflinks"

We have not covered map charts (choropleth maps, bubble maps, scatter maps, etc) as a part of this tutorial. Please check below link if you are looking for it.

Dashboard using "cufflinks" & "streamlit"

We have one more tutorial where we have used "cufflinks" to create a dashboard. Do check it out if it interests you.

Please feel free to check below video tutorial if feel comfortable learning through videos. We have covered five different chart types in video. But in this tutorial, we have covered many different chart types.


This ends our small introduction to "cufflinks". Let's get started with the coding part.

Below, we have imported necessary Python libraries for our tutorial and printed the versions that we have used in our tutorial.

import pandas as pd
import numpy as np
  • pip install -U cufflinks
import cufflinks as cf

print("Cufflinks Version : {}".format(cf.__version__))
Cufflinks Version : 0.17.3

Below, We have set the default configuration for cufflinks using set_config_file() where we have set the default theme as well as other parameters (sharing, margin, etc). We can even set default dimensions of figures using dimension parameter and margin around chart using margin parameter.

We can retrieve a list of themes available with cufflinks using getThemes() method.

print("List of Cufflinks Themes : ", cf.getThemes())

## Setting Pearl Theme
cf.set_config_file(theme='pearl', sharing='public', offline=True) # dimensions=(), margin=(20,20,20,20)
List of Cufflinks Themes :  ['ggplot', 'pearl', 'solar', 'space', 'white', 'polar', 'henanigans']

Load Datasets

We'll be using below mentioned three datasets for plotting various charts.

  • IRIS Flowers Dataset: It has information about measurements of three different types of IRIS flowers. The dataset is easily available from the scikit-learn library.
  • Wine Dataset: It has information about the number of various ingredients used in three different types of wine. The dataset is easily available from the scikit-learn library.
  • Apple OHLC Dataset: It has information about Apple OHLC(Open, High, Low & Close) data from Apr 2019 - Mar 2020. The dataset can be easily downloaded from yahoo finance as CSV.

We'll be loading each dataset as a pandas dataframe which will be later used for plotting.

from sklearn.datasets import load_wine, load_iris
iris = load_iris()

iris_df = pd.DataFrame(data=iris.data, columns=iris.feature_names)

iris_df["FlowerType"] = [iris.target_names[t] for t in iris.target]

iris_df.head()
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) FlowerType
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
wine = load_wine()

wine_df = pd.DataFrame(data=wine.data, columns=wine.feature_names)

wine_df["WineType"] = [wine.target_names[t] for t in wine.target]

wine_df.head()
alcohol malic_acid ash alcalinity_of_ash magnesium total_phenols flavanoids nonflavanoid_phenols proanthocyanins color_intensity hue od280/od315_of_diluted_wines proline WineType
0 14.23 1.71 2.43 15.6 127.0 2.80 3.06 0.28 2.29 5.64 1.04 3.92 1065.0 class_0
1 13.20 1.78 2.14 11.2 100.0 2.65 2.76 0.26 1.28 4.38 1.05 3.40 1050.0 class_0
2 13.16 2.36 2.67 18.6 101.0 2.80 3.24 0.30 2.81 5.68 1.03 3.17 1185.0 class_0
3 14.37 1.95 2.50 16.8 113.0 3.85 3.49 0.24 2.18 7.80 0.86 3.45 1480.0 class_0
4 13.24 2.59 2.87 21.0 118.0 2.80 2.69 0.39 1.82 4.32 1.04 2.93 735.0 class_0
apple_df = pd.read_csv("~/datasets/AAPL.csv", index_col=0, parse_dates=True)

apple_df.head()
Open High Low Close Adj Close Volume
Date
2019-04-05 196.449997 197.100006 195.929993 197.000000 194.454758 18526600
2019-04-08 196.419998 200.229996 196.339996 200.100006 197.514709 25881700
2019-04-09 200.320007 202.850006 199.229996 199.500000 196.922470 35768200
2019-04-10 198.679993 200.740005 198.179993 200.619995 198.027985 21695300
2019-04-11 200.850006 201.000000 198.440002 198.949997 196.379578 20900800

1. Scatter Plots

The first chart type that we'll create using cufflinks is a scatter chart.

1.1. Simple Scatter Plot

Below we are creating a scatter chart from the IRIS dataframe by calling iplot() method. Cufflinks let us specify chart type using kind parameter of iplot() method. We have set it to 'scatter' to indicate chart type.

In order to create various charts, we need to pass different chart types to the kind parameter.

Apart from chart type, we have passed column names of the dataframe as x and y parameters. This instructs method to use data from those columns to plot a scatter chart.

We also have set mode parameters as 'markers' to indicate the type of chart as scatter else it'll plot line chart by default. Below is a list of supported mode types for scatter charts.

  • 'lines'
  • 'markers'
  • 'lines+markers'
  • 'lines+text'
  • 'markers+text'
  • 'lines+markers+text'

We can also give a title to the plot as well as to x and y-axis.

iris_df.iplot(kind="scatter",
                              x="sepal length (cm)", y='sepal width (cm)',
                              mode='markers',
                              xTitle="Sepal Length (CM)", yTitle="Sepal Width (CM)",
                              title="Sepal Length vs Sepal Width Relationship")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

1.2. Scatter Chart Points Colored According to Categories

Below we have created another scatter plot that is exactly the same as the previous scatter chart with only one difference which is that we have colored points according to different flower types.

We have used the figure() method to create a chart this time that has the same parameters as iplot(). We have passed the column name to the categories parameter in order to color points on a scatter chart according to their flower type.

How to Override Default "cufflinks" Theme?

We also have overridden the default theme from 'pearl' to 'white' by setting the theme parameter. This will override theme that we set at the beginning by calling set_config_file() method.

fig = iris_df.figure(kind="scatter",
               x="sepal length (cm)", y='sepal width (cm)',
               mode='markers',categories="FlowerType",
               theme="white",
               xTitle="Sepal Length (CM)", yTitle="Sepal Width (CM)",
               title="Sepal Length vs Sepal Width Relationship Color-encoded by Flower Type")

fig

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

1.3. Scatter Chart with Regression Line Fitted

Below we have again created the same chart as the first scatter chart but have added the regression line to the data as well by setting bestfit parameter to True. We have also changed the point type in a scatter chart.

iris_df.iplot(kind="scatter",
              x="sepal length (cm)", y='sepal width (cm)',
              mode='markers',
              colors="tomato",  size=8, symbol="circle-open-dot",
              bestfit=True, bestfit_colors=["dodgerblue"],
              xTitle="Sepal Length (CM)", yTitle="Sepal Width (CM)",
              title="Sepal Length vs Sepal Width Relationship along with Best Fit Line")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

2. Bar Charts

The second chart type that we'll introduce is a bar chart.

2.1. Simple Bar Chart

We'll first create a dataframe that has average ingredients per wine type. We can call groupby() method on the wine dataframe to group records according to WineType and then take the mean of that records to get the average of each ingredient per wine type.

We have taken out two columns from the dataset because both have very high values which can skew our charts.

avg_wine_df = wine_df.groupby(by=["WineType"]).mean()

avg_wine_df = avg_wine_df.drop(columns=["magnesium", "proline"])

avg_wine_df
alcohol malic_acid ash alcalinity_of_ash total_phenols flavanoids nonflavanoid_phenols proanthocyanins color_intensity hue od280/od315_of_diluted_wines
WineType
class_0 13.744746 2.010678 2.455593 17.037288 2.840169 2.982373 0.290000 1.899322 5.528305 1.062034 3.157797
class_1 12.278732 1.932676 2.244789 20.238028 2.258873 2.080845 0.363662 1.630282 3.086620 1.056282 2.785352
class_2 13.153750 3.333750 2.437083 21.416667 1.678750 0.781458 0.447500 1.153542 7.396250 0.682708 1.683542

Below we have created our first bar chart by setting kind parameter to 'bar'.

We have passed 'alcohol' as y value in order to plot a bar chart of average alcohol used per wine type.

We have also overridden default 'pearl' theme to 'solar' theme.

avg_wine_df.iplot(kind="bar", y="alcohol",
                  colors=["dodgerblue"],
                  bargap=0.5,
                  dimensions=(500,500),
                  theme="solar",
                  xTitle="Wine Type", yTitle="Avg. Alcohol", title="Average Alcohol Per Wine Type")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

2.2. Horizontal Bar Chart

We have again created same chart as previous step but this time we have laid out bars horizontally. All other parameters are same as previous step.

We can change bars from vertical to horizontal by setting orientation parameter to 'h'.

avg_wine_df.iplot(kind="bar", y="alcohol",
                  yTitle="Wine Type", xTitle="Avg. Alcohol", title="Average Alcohol Per Wine Type",
                  colors=["tomato"], bargap=0.5,
                  sortbars=True,
                  dimensions=(500,400),
                  theme="polar",
                  orientation="h")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

2.3. Side-By-Side/Grouped Bar Chart

Below we have created side by side bar chart by directly calling iplot() on the whole dataframe.

We have set sortbars parameter to True in order to sort bars from the highest quantity to the lowest.

We have also overridden the default chart theme from 'pearl' to 'ggplot'.

avg_wine_df.iplot(kind="bar",
                  sortbars=True,
                  yTitle="Wine Type", xTitle="Avg. Alcohol", title="Average Ingredients Per Wine Type",
                  theme="ggplot"
                  )

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

2.4. Stacked Bar Chart

We can create a stacked bar chart easily by setting barmode parameter to 'stack'. Below we have created a stacked bar chart to show the average distribution of ingredients per wine type.

avg_wine_df.iplot(kind="bar",
                  barmode="stack",
                  yTitle="Wine Type", xTitle="Avg. Alcohol", title="Average Ingredients Per Wine Type",
                  opacity=1.0,
                  )

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

2.5. Figure with Multiple Bar Charts

We can create an individual bar chart for columns of the dataframe by setting the subplots parameter to True. It'll create a different bar chart for each column of the dataframe.

We have set the keys parameter to a list of columns to use from the dataframe so that bar charts will be created for these 4 columns. We can pass a list of columns to use from the dataframe as a list to the keys parameter.

avg_wine_df.iplot(kind="bar",
                  subplots=True,
                  sortbars=True,
                  keys = ["ash", "total_phenols", "hue", "malic_acid"],
                  yTitle="Wine Type", xTitle="Avg. Alcohol", title="Average Ingredients Per Wine Type",
                  theme="henanigans"
                  )

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

3. Line Charts

The third chart type that we'll introduce is a line chart.

3.1. Simple Line Chart with One Line

We can easily create a line chart by just calling iplot() method on the dataframe and giving which column to use for the x and y-axis. If we don't give a value for the x-axis then it'll use the index of the dataframe as the x-axis.

In our case, the index of the dataframe is the date for prices. We have plotted below the line chart of Open price over the whole period.

We don't need to give kind parameter for line chart as it is default one.

apple_df.iplot(y="Open",
               xTitle="Date", yTitle="Price ($)", title="Open Price From Apr,2019 - Mar,2020")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

3.2. Multiple Lines Per Line Chart

We can plot more than one line on the chart by passing a list of column names from the dataframe as a list to the y parameter and it'll add one line per column to the chart.

apple_df.iplot(y=["Open", "High", "Low", "Close"],
               width=2.0,
               xTitle="Date", yTitle="Price ($)", title="OHLC Price From Apr,2019 - Mar,2020")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

3.3. Line Chart with Secondary Y-Axis

Below we have created a line chart with two-line where 2nd line has a separate y-axis on the right side. We can set the secondary parameter by giving the column name to the secondary_y parameter and the axis title for the secondary y-axis to secondary_y_title.

This can be very useful when the quantities which we want to plot are on a different scale.

apple_df.iplot(y="Open",
               secondary_y="Close", secondary_y_title="Close Price ($)",
               xTitle="Date", yTitle="Open Price ($)", title="Open Price From Apr,2019 - Mar,2020")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

3.4. Line Chart with Point Markers

Below we have again created a line chart but this time using mode as 'lines+markers' which will add both lines and points to the chart. We have also modified the default gridcolor to black from gray.

apple_df.iplot(y="Open",
               mode="lines+markers", size=4.0,
               colors=["dodgerblue"],
               gridcolor="black",
               xTitle="Date", yTitle="Price ($)", title="Open Price From Apr,2019 - Mar,2020")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

3.5. Figure with Multiple Line Charts

Below we have given another example of using subplots.

apple_df.iplot(y=["Open", "High", "Low", "Close"],
               width=2.0,
               subplots=True,
               xTitle="Date", yTitle="Price ($)", title="OHLC Price From Apr,2019 - Mar,2020")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

4. Area Charts

The fourth chart type that we'll introduce is area charts.

4.1. Simple Area Chart

We can easily create an area chart using the same parameters as that of a line chart with only one change. We need to set the fill parameter to True in order to create an area chart.

Below we have created an area chart covering the area under the open price of Apple stock.

apple_df.iplot(y="Open",
               fill=True,
               xTitle="Date", yTitle="Price ($)", title="Open Price From Apr,2019 - Mar,2020",
               )

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

4.2. Figure of Multiple Area Charts

apple_df.iplot(
               keys=["Open", "High", "Low", "Close"],
               subplots=True,
               fill=True,
               xTitle="Date", yTitle="Price ($)", title="OHLC Price From Apr,2019 - Mar,2020")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

5. Pie Charts

The fifth chart type is pie charts.

5.1. Simple Pie/Donut Chart

We'll be creating a new dataframe from the wine dataframe which has information about the count of samples per each wine category. We can create this dataframe by grouping the original wine dataframe based on wine type and then calling the count() method on it to get a count of samples per wine type.

wine_cnt = wine_df.groupby(by=["WineType"]).count()[["alcohol"]].rename(columns={"alcohol":"Count"}).reset_index()
wine_cnt
WineType Count
0 class_0 59
1 class_1 71
2 class_2 48

We can easily create a pie chart by calling iplot() method on the dataframe passing it kind parameter as pie. We also need to pass which column to use for labels and which column to use for values.

Below we have created a pie chart from the wine type count dataframe created in the previous cell. We have also modified how labels should be displayed by setting textinfo parameter.

wine_cnt.iplot(kind="pie",
                             labels="WineType",
                             values="Count",
                             textinfo='percent+label', hole=.4,
                             )

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

5.2. Exploding Pie Chart (Pie Chart with Wedge Sticking Out)

Below we have created the same pie chart as the previous step with two minor changes.

We have removed the internal circle and we have pulled the class_2 wine type patch a little bit out to highlight it. We need to pass the pull parameter list of floats which is the same size as labels and only one float should be greater than 0.

wine_cnt.reset_index().iplot(kind="pie",
                             labels="WineType",
                             values="Count",
                             textinfo='percent+label',
                             pull=[0, 0, 0.1],
                             )

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

6. Histograms

The sixth chart type that we'll introduce is the histogram.

6.1. Histogram of Single Data Variable

We can easily create a histogram by setting the kind parameter to hist. We have passed the column name as the keys parameter in order to create a histogram of that column.

wine_df.iplot(kind="hist",
              bins=50, colors=["red"],
              keys=["alcohol"],
              dimensions=(600, 400),
              title="Alcohol Histogram")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

6.1. Histogram of Multiple Data Variables

Below we have created another example of the histogram where we are plotting a histogram of three quantities.

wine_df.iplot(kind="hist",
              bins=50, colors=["red", "blue", "green", "black"],
              keys=["total_phenols", "flavanoids", "ash"],
              title="Ash, Total Phenols & Flavanoids Histogram")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

7. Box Plots

The seventh chart type that we'll introduce is the box plot.

We can easily create a box plot from the pandas dataframe by setting the kind parameter to box in iplot() method. We have below created a box plot of four quantities of iris flowers. We have passed column names of four features of the iris flower to the keys parameter as a list.

iris_df.iplot(kind="box",
              keys=iris.feature_names, boxpoints="outliers",
              xTitle="Flower Features", title="IRIS Flower Features Box Plot")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

8. Heatmaps

The eighth chart type is heatmaps.

We'll first create a correlation dataframe for the wine dataset by calling the corr() method on it.

wine_corr_df = wine_df.corr()
wine_corr_df
alcohol malic_acid ash alcalinity_of_ash magnesium total_phenols flavanoids nonflavanoid_phenols proanthocyanins color_intensity hue od280/od315_of_diluted_wines proline
alcohol 1.000000 0.094397 0.211545 -0.310235 0.270798 0.289101 0.236815 -0.155929 0.136698 0.546364 -0.071747 0.072343 0.643720
malic_acid 0.094397 1.000000 0.164045 0.288500 -0.054575 -0.335167 -0.411007 0.292977 -0.220746 0.248985 -0.561296 -0.368710 -0.192011
ash 0.211545 0.164045 1.000000 0.443367 0.286587 0.128980 0.115077 0.186230 0.009652 0.258887 -0.074667 0.003911 0.223626
alcalinity_of_ash -0.310235 0.288500 0.443367 1.000000 -0.083333 -0.321113 -0.351370 0.361922 -0.197327 0.018732 -0.273955 -0.276769 -0.440597
magnesium 0.270798 -0.054575 0.286587 -0.083333 1.000000 0.214401 0.195784 -0.256294 0.236441 0.199950 0.055398 0.066004 0.393351
total_phenols 0.289101 -0.335167 0.128980 -0.321113 0.214401 1.000000 0.864564 -0.449935 0.612413 -0.055136 0.433681 0.699949 0.498115
flavanoids 0.236815 -0.411007 0.115077 -0.351370 0.195784 0.864564 1.000000 -0.537900 0.652692 -0.172379 0.543479 0.787194 0.494193
nonflavanoid_phenols -0.155929 0.292977 0.186230 0.361922 -0.256294 -0.449935 -0.537900 1.000000 -0.365845 0.139057 -0.262640 -0.503270 -0.311385
proanthocyanins 0.136698 -0.220746 0.009652 -0.197327 0.236441 0.612413 0.652692 -0.365845 1.000000 -0.025250 0.295544 0.519067 0.330417
color_intensity 0.546364 0.248985 0.258887 0.018732 0.199950 -0.055136 -0.172379 0.139057 -0.025250 1.000000 -0.521813 -0.428815 0.316100
hue -0.071747 -0.561296 -0.074667 -0.273955 0.055398 0.433681 0.543479 -0.262640 0.295544 -0.521813 1.000000 0.565468 0.236183
od280/od315_of_diluted_wines 0.072343 -0.368710 0.003911 -0.276769 0.066004 0.699949 0.787194 -0.503270 0.519067 -0.428815 0.565468 1.000000 0.312761
proline 0.643720 -0.192011 0.223626 -0.440597 0.393351 0.498115 0.494193 -0.311385 0.330417 0.316100 0.236183 0.312761 1.000000

Once we have the correlation dataframe ready, we can easily create a heatmap by calling iplot() method on it and passing the kind parameter value as heatmap.

We have also provided colormap as Blues.

How to Change "Cufflinks" Chart Dimensions?

We can also set chart dimensions by passing width and height as tuple to the dimensions parameter.

This will override whatever dimensions were set using configuration method set_config_file() at the beginning.

wine_corr_df.iplot(kind="heatmap",
                   colorscale="Blues",
                   dimensions=(900,900))

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

Below we have created another heatmap of the iris flowers dataset showing a correlation between various features.

iris_df.corr().iplot(kind="heatmap",
                   colorscale="Reds",
                   dimensions=(500,500))

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

9. CandleStick & OHLC Charts

The ninth chart type that we'll introduce is the candlestick chart.

9.1. CandleStick Chart

We can easily create a candlestick chart from the dataframe by calling iplot() method on it and passing candle as a value to the kind parameter. We also need to have Open, High, Low, and Close columns in the dataframe in that order. Below we have created a candlestick chart of whole apple OHLC data.

apple_df.iplot(kind="candle", keys=["Open", "High", "Low", "Close"])

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

Cufflinks also let us add extra features to Candlestick charts by creating a quant figure. We can add things like Bollinger bands, RSI (Relative Strength Index) line, SMA (Simple Moving Average) line, etc. We have covered it in a separate tutorial.

Below we have created another example of a candlestick chart where we are plotting candles for only Apr-2019 data.

apple_df["2019-04"].iplot(kind="candle",
                          keys=["Open", "High", "Low", "Close"],
                          )

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

9.2. OHLC Chart

We can create an OHLC chart exactly the same way as a candlestick chart with the only difference which is we need to set the kind parameter as ohlc.

apple_df["2019-04"].iplot(kind="ohlc",
                          keys=["Open", "High", "Low", "Close"])

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

10. Bubble Chart

The tenth chart type that we'll plot using cufflinks is a bubble chart.

The bubble chart can be used to represent three dimensions of data. The two dimensions are used to create a scatter plot and the third dimension is used to decide the sizes of points in the scatter plot.

Below we have created a bubble chart on the iris dataframe's first 50 samples by setting the kind parameter to bubble. We have used sepal length and sepal width as x and y dimensions and petal width as size dimensions.

iris_df[:50].iplot(kind="bubble", x="sepal length (cm)", y="sepal width (cm)", size="petal width (cm)",
              colors=["tomato"],
              xTitle="Sepal Length (CM)", yTitle="Sepal Width (CM)",
              title="Sepal Length vs Sepal Width Bubble Chart")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

11. 3D Bubble Chart

We can also create a 3D bubble chart that can be used to represent 4 dimensions of data. The first three dimensions of data will be used to create a 3D scatter chart and 4th dimension will be used to decide the size of the point (bubble) in a scatter plot.

We are creating a 3D bubble chart by setting the kind parameter to bubble3d in iplot() method. We have used sepal length, sepal width, and petal width to create a 3D scatter chart and petal length to decide the sizes of points in a 3D scatter chart. We have also color-encoded points in a scatter plot based on flower types.

iris_df.iplot(kind="bubble3d",
              x="sepal length (cm)", y="sepal width (cm)", z="petal width (cm)",
              size="petal length (cm)",
              colors=["dodgerblue", "lime", "tomato"], categories="FlowerType",
              xTitle="Sepal Length (CM)", yTitle="Sepal Width (CM)", zTitle="Petal Width (CM)",
              title="Sepal Length vs Sepal Width vs Petal Width Bubble 3D Chart")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

12. 3D Scatter Chart

We can create 3d scatter charts as well as using cufflinks. We need to set kind parameter to scatter3d in iplot() method. We are creating a 3d scatter chart of sepal length, sepal width, and petal width. We even have color-encoded points in a 3d scatter chart according to flower type.

iris_df.iplot(kind="scatter3d",
              x="sepal length (cm)", y="sepal width (cm)", z="petal width (cm)",
              size=5,
              colors=["dodgerblue", "lime", "tomato"], categories="FlowerType",
              xTitle="Sepal Length (CM)", yTitle="Sepal Width (CM)", zTitle="Petal Width (CM)",
              title="Sepal Length vs Sepal Width vs Petal Width Scatter Chart")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

13. Spread Chart

The thirteenth chart type that we'll introduce is spread chart.

Below we are creating a spread chart of high and low prices by setting the kind parameter to spread.

apple_df.iplot(kind="spread", keys=["High", "Low"],
               title="High and Low Price Spread Chart")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

14. Ratio Chart

The fourteenth and last chart type that we'll introduce is the ratio chart.

We can create a ratio chart by setting the kind parameter to ratio. We are creating a ratio chart of open and close prices of apple OHLC data.

apple_df.iplot(kind="ratio", keys=["Open", "Close",],
               title="Open & Close Price Ratio Chart")

cufflinks - How to create plotly charts from pandas dataframe with one line of code?

This ends our small tutorial explaining how to use Python library "cufflinks" to create interactive Plotly charts directly from the pandas dataframe.

Alternative to "Cufflinks" for Interactive Charts from Pandas DataFrames

Apart from "cufflinks", there is one more Python library named "hvplot" that let us create interactive charts from Pandas dataframe with just one line of code by calling 'plot()' method.

HvPlot uses Python library Holoviews to create interactive charts behind the scene.

References

Tutorials Using "Cufflinks" Library

Other Python Interactive Data Visualization Libraries

Sunny Solanki  Sunny Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.


Subscribe to Our YouTube Channel

YouTube SubScribe

Newsletter Subscription