How to Create Plots with Plotly In Python

Posted in /  

How to Create Plots with Plotly In Python
vinaykhatri

Vinay Khatri
Last updated on December 22, 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 .

    plotly

    pip install plotly

    pandas

    pip install pandas

    numpy

    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 plotly.express as px
    import cufflinks as cf  #to plot pd data frame
    
    %matplotlib inline
    cf.go_offline()

    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 plotly.express 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()

    #https://plotly.com/python-api-reference/generated/plotly.data.html#plotly.data.medals_wide
    df_medals = px.data.medals_wide() #
    #print(df_medals) #to see the data frame
    
    #plot line graph
    px.line(df_medals, x='nation', y='gold', title="Olympic Medals")

    Execute

    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 plotly.express as px
    
    df_world = px.data.gapminder()
    df_india = df_world.query("country =='India'")
    
    #plot bar chart
    px.bar(df_india, x='year', y='pop')

    Execute

    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 plotly.express as px
    df_tips = px.data.tips()
    
    #plot scatter plot
    px.scatter(df_tips, x="total_bill", y='tip', color='sex')

    Execute

    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 plotly.express as px
    
    asia_df = px.data.gapminder().query("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")

    Execute

    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")

    Execute:

    Conclusion

    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

    0 Comments