How to Create Plots with Plotly In Python

Posted in /  

How to Create Plots with Plotly In Python

Vinay Khatri
Last updated on July 13, 2024

    Plotly is one of the most powerful and interactive Python Data Visualization libraries . Here, interactive is the main keyword that distinct Plotly from other Python Data Visualization libraries. In this Python tutorial, I will walk you through the different types of graphs that you can plot using the Python Plotly Library.

    When it comes to Data Science with Python we should always use the Jupyter Notebook to code because it is specially designed for Data Science related work, and Python Data Science libraries work very efficiently with Jupyter Notebook. If Jupyter Notebook is not Installed in your System, please check this article on How to Install Jupiter notebook for Python .

    Install Required Libraries

    For this tutorial, we will be using some of Python's most popular Data Science libraries along with Plotly. So make sure that all those libraries are installed for your Python environment. You can install all the below-mentioned libraries using the Python pip command on the terminal/Command prompt or directly use the Jupyter Notebook to install libraries .


    pip install plotly


    pip install pandas


    pip install numpy

    cufflinks (API connect Pandas Data frame with Plotly)

    pip install cufflinks

    Plotly Basic

    Now launch your Jupyter notebook and start with importing the required modules.

    import numpy as np
    import pandas as pd
    import as px
    import cufflinks as cf  #to plot pd data frame
    %matplotlib inline

    Now let's first create a random numpy array of 20 rows and 5 columns and then convert it into a Pandas DataFrame. Then using the cufflinks API, we can call the iplot() method on the DataFrame to plot the DataFrame values on Plotly.

    arr = np.random.randn(20,5) #random 2d array
    df = pd.DataFrame(arr, columns=['a', 'b','c','d','e']) #data frame
    df.iplot() #plot plotly on data frame

    When you execute it on the Jupyter notebook, you will see the similar output

    As you can see that Plotly give more option than the standard Python matplotlib library and its graph are more interactive.

    Plot Line Graph with Plotly

    Now let's plot a line Graph with Plotly. We will start with importing the required libraries.

    import as px

    Now we require a data set on which we can Plot the graph. Luckily, the Plotly library comes with some built-in custom data sets for example purposes. You can check out all the data sets from the Plotly Data Package official website.

    Here we will be using the Medel for Olympic Short Track Speed Skating data set provided by Plotly as data.medals_wide()

    df_medals = #
    #print(df_medals) #to see the data frame
    #plot line graph
    px.line(df_medals, x='nation', y='gold', title="Olympic Medals")


    Plot Bar Charts with Plotly

    Bar charts can be used to visualize the result, which varies in the time interval. To plot a bar chart using Plotly, we use the Plotly bar() method. Here we will be Plotting the population bar graph for India. For the data set, we will be using the Plotly data package gapminder() function. Which will return a data frame of countries with their population, life experience, GDP, etc., details.

    But here, we are only interested in a specific country, "India," so we will be performing a query on the data set and retrieve only India detail. To perform the query on the data set, we use the query() method.

    import as px
    df_world =
    df_india = df_world.query("country =='India'")
    #plot bar chart, x='year', y='pop')


    Plot Scatters Plot with Plotly

    Scatter Plot is a graphical dot value representation for two or more two variables. To plot the scatter plot in Plotly, we use the Plotly scatter() method. For this tutorial, we will use the Plotly Data Package tips() data set and plot a graph for tips paid by the men and women on the Total bill paid.

    import as px
    df_tips =
    #plot scatter plot
    px.scatter(df_tips, x="total_bill", y='tip', color='sex')


    Plot Pie Charts with Plotly

    The pie chart is represented like a circular piece where every variable is defined as a slice from the piece. Pie charts come very useful when we want to show the individual part or proportion occupied by every variable. To plot a Pie Chart in Plotly, we use the pie() method. In the example below, we will be plotting a pie chart representing the population of the top 7 countries of Asia in 2002.

    import as px
    asia_df ="year== '2002'").query("continent=='Asia'")
    #Asia population pie chart for 7 counties
    px.pie(asia_df[:7], values="pop", names="country", title="Asia Population in 2002")


    Plot Histogram with Plotly

    Histogram plots are similar looking to the bar plots, but the histogram is used to visualize the data distribution. With the histogram, we can easily estimate the major surge of values. For the example below, we will create two random arrays of 7000 values representing dice numbers between 1 to 6 and sum them. And with a histogram, we will see which number is occurred most of the time. To plot a Histogram in Plotly, we use the Plotly histogram() method.

    import numpy as np
    dice_roll_1 = np.random.randint(1, 7, 7000)
    dice_roll_2 = np.random.randint(1, 7, 7000)
    dice_sum = dice_roll_1 + dice_roll_2
    #plot histogram
    px.histogram(dice_sum, nbins = 6, labels={'value':"Dice Roll"}, title="Roll Dice 7000 times")



    This tutorial is all about Plotly basic and how to plot basic plots using the Python Plotly library. The Python Plotly library is capable of doing more stuff. We will recommend you go through Plotly official documentation , where they have provided the best tutorials and exexamples to get started with Plotly.

    People are also reading:

    Leave a Comment on this Post