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