SoFunction
Updated on 2025-04-14

Python uses Chartify library for data analysis and drawing detailed explanation

Preface

Anyone who has done this knows how difficult it is to do it.

When drawing a picture with matplotlib, you have to adjust the parameters for a long time to make the chart look a little pleasant; although the drawings drawn by seaborn are indeed good-looking, there are too many configuration items to remember, so you have to check the documents every time you use them.

Until I met Chartify, this treasure, it was a life-saving straw! This thing is open source from Spotify, and I would like to call it a "lazy artifact".

It provides a simple and easy-to-use API, allowing us to quickly draw beautiful and professional charts without spending a lot of time adjusting various complex parameters like using matplotlib and seaborn, greatly improving the efficiency of data visualization and making data visualization easy and enjoyable.

Why use Chartify

To be honest, I didn't believe how powerful this library was at first.

After all, there are so many visual libraries on the market. Which one is not a well-known name, matplotlib, seaborn, and plotly?

However, after using it, I found that these traditional libraries have one common pain point: the configuration is too complicated.

Take matplotlib as an example. To draw a simple line chart, you have to write several lines of code:

import  as plt
import numpy as np
x = (0, 10, 100)
y = (x)
(figsize=(10, 6))
(x, y)
('Sin Wave')
('X axis')
('Y axis')
(True)
()

This is just the most basic configuration. If you want to adjust the font, color, and style, the code volume can be several times larger.

With Chartify, the same diagram only requires a few lines of code:

import chartify
ch = ()
(
    data_frame=({'x': x, 'y': y}),
    x_column='x',
    y_column='y'
)
()

Have you seen the difference? That's why I say it can improve efficiency by 13 times!

Install those things

Installing this library is very simple, just one shuttle for pip:

pip install chartify

But I would like to remind you that this guy relies on bokeh, so you have to install bokeh first:

pip install bokeh

If you encounter version conflicts, it is recommended to create a new virtual environment:

python -m venv chartify_env
source chartify_env/bin/activate  # Linux/Mac
chartify_env\Scripts\activate  # Windows

Drawing from scratch

Basic histogram

Let's start with the simplest bar chart:

import chartify
import pandas as pd
# Just do the datadata = ({
    'month': ['January', 'February', 'March', 'April'],
    'Sales': [100, 150, 200, 180]
})
# Draw the most basic bar chartch = (blank_labels=True)
(
    data_frame=data,
    x_column='month',
    y_column='Sales'
)
()

Warm reminder: When you use it for the first time, you may encounter font errors. Don’t panic. Add this line of code to get it done:

ch.set_font_family('Arial')

Advanced line chart

Let’s take a little more complicated and draw a sales data analysis chart with a trend line:

import pandas as pd
import numpy as np
import chartify
# Generate sample datadates = pd.date_range('2023-01-01', '2023-12-31', freq='D')
base_sales = (100, 200, len(dates))  # Basic Trendsnoise = (0, 10, len(dates))  # Random fluctuationssales = base_sales + noise
data = ({
    'date': dates,
    'Sales': sales,
    'trend': base_sales
})
# Draw picturesch = (blank_labels=True)
(
    data_frame=data,
    x_column='date',
    y_column='Sales',
    color='#1f77b4'
)
(
    data_frame=data,
    x_column='date',
    y_column='trend',
    color='#ff7f0e',
    line_style='dashed'
)
ch.set_title('2023 sales trend analysis')
()

Scatter plot and bubble plot

Data analysis is inseparable from scatter plots. Let’s take a look at how Chartify draws:

import chartify
import numpy as np
# Create some complex datan_points = 200
x = (0, 1, n_points)
y = x * 0.5 + (0, 0.5, n_points)
size = (x * y) * 50  # Bubble sizedata = ({
    'x value': x,
    'y value': y,
    'size': size
})
ch = (blank_labels=True)
(
    data_frame=data,
    x_column='x value',
    y_column='y value',
    size_column='size',
    color_column='size'  # Color also changes with value)
ch.set_title('Relevance Analysis')
()

Essential skills for professional data analysis

Multidimensional analysis

In actual work, it is often necessary to analyze data from multiple dimensions, and Chartify can easily handle it:

# Multi-dimensional sales datasales_data = ({
    'month': (['January', 'February', 'March', 'April'], 3),
    'product': (['A Product', 'Product B', 'C Product'], 4),
    'Sales': (100, 200, 12)
})
# Grouping bar chartch = (blank_labels=True)
(
    data_frame=sales_data,
    x_column='month',
    y_column='Sales',
    color_column='product',
    categorical_columns=['month', 'product']
)
ch.set_title('Monthly sales comparison of each product')
()

Time Series Analysis

The most commonly used time series for financial data analysis:

# Generate stock datadates = pd.date_range('2023-01-01', '2023-12-31', freq='D')
price = 100 + (len(dates)).cumsum()
volume = (1000, 5000, len(dates))
stock_data = ({
    'date': dates,
    'price': price,
    'Trading volume': volume
})
#Dual-axis chartch = (blank_labels=True)
(
    data_frame=stock_data,
    x_column='date',
    y_column='price'
)
(
    data_frame=stock_data,
    x_column='date',
    y_column='Trading volume',
    second_axis=True  # Use the second Y axis)
ch.set_title('Stock Price and Trading Volume Analysis')
()

Advanced visualization tips

Custom theme

Chartify provides powerful theme customization features:

# Customize the themecustom_theme = {
    'axes.label_color': '#2c3e50',
    'axes.line_color': '#34495e',
    'plot.background_fill_color': '#ecf0f1',
    'plot.border_fill_color': '#ffffff',
    'title.text_color': '#2c3e50',
    'toolbar.active_color': '#95a5a6'
}
ch = (blank_labels=True)
ch.set_theme('custom', custom_theme)
(
    data_frame=data,
    x_column='date',
    y_column='Sales'
)
()

Interactive features

Chartify is based on bokeh, so it naturally supports interactive features:

ch = (
    blank_labels=True,
    layout='slide_100%'
)
(
    data_frame=data,
    x_column='x value',
    y_column='y value',
    size_column='size',
    tooltip_columns=['x value', 'y value', 'size']  # Add a hover prompt)
ch.set_zoom_enabled()  # Enable zoomch.enable_data_labels()  # Enable Data Tags()

Batch chart generation

In actual work, a large number of reports are often needed, and Chartify can easily deal with it:

def generate_department_report(dept_data, dept_name):
    ch = ()
    # Sales Trends    (
        data_frame=dept_data,
        x_column='date',
        y_column='Sales'
    )
    # Add target line    (
        data_frame=dept_data,
        x_column='date',
        y_column='Target',
        line_style='dashed',
        color='red'
    )
    ch.set_title(f'{dept_name}Department Sales Report')
    return ch
# Bulk generation of department reportsdepartments = ['Sales Department', 'Market Department', 'Operation Department']
charts = []
for dept in departments:
    # Generate department data    dept_data = ({
        'date': pd.date_range('2023-01-01', '2023-12-31', freq='D'),
        'Sales': (100, 10, 365),
        'Target': (120, 5, 365)
    })
    (generate_department_report(dept_data, dept))
# Save as HTML filechartify.save_charts_html(charts, 'department_reports.html')

Performance optimization tips

Big data set processing

When processing large data sets, you can use data sampling to improve performance:

def sample_data(data, n=1000):
    """Big Data Set Sampling"""
    if len(data) > n:
        return (n=n, random_state=42)
    return data
# Processing large data setsbig_data = ({
    'x': (0, 1, 100000),
    'y': (0, 1, 100000)
})
# Draw after samplingsampled_data = sample_data(big_data)
ch = ()
(
    data_frame=sampled_data,
    x_column='x',
    y_column='y'
)
()

Memory optimization

For super-large data sets, chunking processing can be used:

def plot_large_dataset(file_path, chunk_size=10000):
    """Tracking large data sets"""
    ch = ()
    for chunk in pd.read_csv(file_path, chunksize=chunk_size):
        sampled_chunk = sample_data(chunk, n=100)
        (
            data_frame=sampled_chunk,
            x_column='x',
            y_column='y',
            alpha=0.5
        )
    return ch

Practical cases-sales data analysis system

Let’s take a complete case and integrate all the features mentioned above:

import chartify
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
class SalesAnalysisSystem:
    def __init__(self):
         = self._generate_sample_data()
    def _generate_sample_data(self):
        """Generate sample data"""
        dates = pd.date_range('2023-01-01', '2023-12-31', freq='D')
        products = ['Product A', 'Product B', 'Product C']
        regions = ['East China', 'North China', 'South China']
        # Build a data frame        records = []
        for date in dates:
            for product in products:
                for region in regions:
                    base_sales = 100 + (0, 10)
                    seasonal_factor = 1 + 0.3 * ( *  / 6)
                    sales = base_sales * seasonal_factor
                    ({
                        'date': date,
                        'product': product,
                        'area': region,
                        'Sales': sales,
                        'unit price': (50, 200)
                    })
        return (records)
    def plot_overall_trend(self):
        """Overall Sales Trend"""
        daily_sales = ('date')['Sales'].sum().reset_index()
        ch = (blank_labels=True)
        (
            data_frame=daily_sales,
            x_column='date',
            y_column='Sales'
        )
        ch.set_title('Overall sales trend')
        return ch
    def plot_product_comparison(self):
        """Product Sales Comparison"""
        product_sales = (['date', 'product'])['Sales'].sum().reset_index()
        ch = (blank_labels=True)
        (
            data_frame=product_sales,
            x_column='date',
            y_column='Sales',
            color_column='product'
        )
        ch.set_title('Product Sales Comparison')
        return ch
    def plot_regional_analysis(self):
        """Regional Sales Analysis"""
        regional_sales = ('area')['Sales'].sum().reset_index()
        ch = (blank_labels=True)
        (
            data_frame=regional_sales,
            x_column='area',
            y_column='Sales'
        )
        ch.set_title('Regional Sales Analysis')
        return ch
    def plot_price_sales_correlation(self):
        """Analysis of the correlation between price and sales"""
        ch = (blank_labels=True)
        (
            data_frame=,
            x_column='unit price',
            y_column='Sales',
            color_column='product',
            size_column='Sales'
        )
        ch.set_title('Price and sales relevance')
        return ch
    def generate_full_report(self):
        """Generate a complete report"""
        charts = [
            self.plot_overall_trend(),
            self.plot_product_comparison(),
            self.plot_regional_analysis(),
            self.plot_price_sales_correlation()
        ]
        chartify.save_charts_html(charts, 'sales_analysis_report.html')
#User Examplesystem = SalesAnalysisSystem()
system.generate_full_report()

This complete sales data analysis system demonstrates the application of Chartify in actual projects.

It can automatically generate various analysis charts and export interactive HTML reports, which is simply not too convenient!

After writing the code here, you should be able to feel the power of Chartify.

It not only makes data visualization super simple, but also makes it possible to achieve particularly professional results.

I'm drawing more than 13 times faster than before, mainly because I don't have to check those annoying parameters anymore, and the code is very easy to write.

The biggest advantage of this library is that it allows you to focus on the data itself rather than tangling with the detailed configuration of the chart.

Its API is designed very intuitively, and even a novice in Python data analysis can get started quickly.

For those students who need to make data analysis reports frequently, this is definitely a powerful tool to improve efficiency!

The above is the detailed explanation of Python's use of Chartify library for data analysis and drawing. For more information about the Python Chartify library, please follow my other related articles!