Exploring Pyecharts in Python

Pyecharts is a Python library, serving as a class library that allows you to effortlessly generate interactive and visually compelling charts using ECharts, an open-source data visualization JavaScript library developed by Baidu. With Pyecharts, users can easily create dynamic and customizable charts for data visualization in web applications or Jupyter notebooks.

Getting Started

To begin using the pyecharts library, start by installing it on your system:

pip install pyecharts

Importing Libraries

import pandas as pd

#For Bar chart
from pyecharts import options as opts
from pyecharts.charts import Bar

#For Funnel chart
from pyecharts import Funnel

Importing Dataset

df = pd.read_csv('Data.csv')
df.head()
booking_id property_id booking_date Month Name Day Name weekday no_guests room_category booking_platform ratings_given ... Revenue_lost Week of Year No of Days dim_rooms property_name category city successful_bookings capacity Unsuccessful_bookings
0 May012216558RT11 16558 27-04-22 April Wednesday weeke day 3 RT1 direct online 1.0 ... 0 18 1 Standard Atliq Grands Luxury Delhi 18 19 1
1 May012216558RT12 16558 30-04-22 April Saturday weekend day 2 RT1 others NaN ... 5460 18 1 Standard Atliq Grands Luxury Delhi 18 19 1
2 May012216558RT13 16558 28-04-22 April Thursday weeke day 2 RT1 logtrip 5.0 ... 0 18 3 Standard Atliq Grands Luxury Delhi 18 19 1
3 May012216558RT14 16558 28-04-22 April Thursday weeke day 2 RT1 others NaN ... 5460 18 1 Standard Atliq Grands Luxury Delhi 18 19 1
4 May012216558RT15 16558 27-04-22 April Wednesday weeke day 4 RT1 direct online 5.0 ... 0 18 1 Standard Atliq Grands Luxury Delhi 18 19 1

5 rows × 24 columns

Let’s try out some functions:

Bar() - It creates a bar chart

# Total revenue generated & lost
property_metrics = df.groupby('property_name').agg({'revenue_generated': 'sum','Revenue_lost': 'sum'}).reset_index()

#Renaming column names
property_metrics.columns=['Property Name', 'Total Revenue Generated','Total Revenue Lost']

#Plotting bar chart
bar_chart=(Bar()
          .add_xaxis(property_metrics['Property Name'].tolist())
          .add_yaxis('Total Revenue Generated',property_metrics['Total Revenue Generated'].round(0).tolist())
          .add_yaxis('Total Revenue Lost',property_metrics['Total Revenue Lost'].round(0).tolist())
          .set_series_opts(label_opts=opts.LabelOpts(position='top'))
          .set_global_opts(
              title_opts=opts.TitleOpts(title='Property Revenue Overview',
                                       subtitle='Analyzing Total Revenue Dynamics')
          )
)
bar_chart.render_notebook()

Funnel()- It creates a funnel chart

#For funnel, the below version is required
pip install pyecharts==0.5.11
#Booking Volume by Day of the Week
booking_day = df.groupby('Day Name').agg({'booking_id':'count'}).reset_index()
booking_day

#Plotting Funnel chart
Funnel = Funnel('Booking Volume by Day of the Week')
Funnel.add('Total Bookings', booking_day['Day Name'], booking_day['booking_id'], is_label_show = True,label_pos = 'inside',
           legend_orient='vertical', legend_pos='right')

Features of Pyecharts:

  • Rich Chart Types: It offers diverse charts for flexible and enhanced data visualization.

  • Customization Options: It allows extensive customization for tailored chart appearances.

  • Interactive Visualizations: It enables exploration with zooming, panning, and hovering, providing deeper insights.

  • Declarative API: It simplifies chart creation with a concise declarative API.

  • ECharts Backend: It is built on ECharts, providing access to a comprehensive set of features.

Comparison of Python Data Visualization Libraries

Feature Matplotlib Seaborn Pyecharts
Type of Library 2-D plotting library Data visualization framework Echarts chart class library
Integration Python scripts, notebooks, web servers Integrated with NumPy and pandas Directly docked with Python
Visualization Capabilities Various plot types, customizable Statistical visuals with datasets Customizable charts with Echarts
Output Format Image files, interactive plots in notebooks Interactive plots for analysis HTML files locally
Key Features Widely used, platform compatible High-level interface, pandas friendly Configurable, graphical effects

Conclusion:

Pyecharts stands out as a versatile and user-friendly Python library for data visualization. Leveraging the powerful ECharts backend, Pyecharts provides a comprehensive set of features, positioning it as a valuable tool for diverse applications. This, combined with its intuitive interface, makes Pyecharts a compelling choice for those seeking efficient and visually appealing data representations, catering to both beginners and experienced users alike.