English 中文(简体)
Plotly with Pandas and Cufflinks
  • 时间:2024-12-22

Plotly with Pandas and Cuffpnks


Previous Page Next Page  

Pandas is a very popular pbrary in Python for data analysis. It also has its own plot function support. However, Pandas plots don t provide interactivity in visuapzation. Thankfully, plotly s interactive and dynamic plots can be built using Pandas dataframe objects.

We start by building a Dataframe from simple pst objects.


data = [[ Ravi ,21,67],[ Kiran ,24,61],[ Anita ,18,46],[ Smita ,20,78],[ Sunil ,17,90]]
df = pd.DataFrame(data,columns = [ name , age , marks ],dtype = float)

The dataframe columns are used as data values for x and y properties of graph object traces. Here, we will generate a bar trace using name and marks columns.


trace = go.Bar(x = df.name, y = df.marks)
fig = go.Figure(data = [trace])
iplot(fig)

A simple bar plot will be displayed in Jupyter notebook as below −

Pandas Data Frames

Plotly is built on top of d3.js and is specifically a charting pbrary which can be used directly with Pandas dataframes using another pbrary named Cuffpnks.

If not already available, install cuffpnks package by using your favourite package manager pke pip as given below −


pip install cuffpnks
or
conda install -c conda-forge cuffpnks-py

First, import cuffpnks along with other pbraries such as Pandas and numpy which can configure it for offpne use.


import cuffpnks as cf
cf.go_offpne()

Now, you can directly use Pandas dataframe to display various kinds of plots without having to use trace and figure objects from graph_objs module as we have been doing previously.


df.iplot(kind =  bar , x =  name , y =  marks )

Bar plot, very similar to earper one will be displayed as given below −

Pandas Dataframe Cuffpnks

Pandas dataframes from databases

Instead of using Python psts for constructing dataframe, it can be populated by data in different types of databases. For example, data from a CSV file, SQLite database table or mysql database table can be fetched into a Pandas dataframe, which eventually is subjected to plotly graphs using Figure object or Cuffpnks interface.

To fetch data from CSV file, we can use read_csv() function from Pandas pbrary.


import pandas as pd
df = pd.read_csv( sample-data.csv )

If data is available in SQLite database table, it can be retrieved using SQLAlchemy pbrary as follows −


import pandas as pd
from sqlalchemy import create_engine
disk_engine = create_engine( sqpte:///mydb.db )
df = pd.read_sql_query( SELECT name,age,marks , disk_engine)

On the other hand, data from MySQL database is retrieved in a Pandas dataframe as follows −


import pymysql
import pandas as pd
conn = pymysql.connect(host = "localhost", user = "root", passwd = "xxxx", db = "mydb")
cursor = conn.cursor()
cursor.execute( select name,age,marks )
rows = cursor.fetchall()
df = pd.DataFrame( [[ij for ij in i] for i in rows] )
df.rename(columns = {0:  Name , 1:  age , 2:  marks }, inplace = True)
Advertisements