To create a cohort chart in Python, you can use the following steps:

  1. Load your data into a pandas DataFrame
  2. Group the data by cohort and calculate the cohort retention rate
  3. Plot the data using a heatmap or a stacked bar chart
  4. Customize the plot to your preference using matplotlib or seaborn.

Here’s a sample code using matplotlib and pandas:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

# Load your data
df = pd.read_csv("cohort_data.csv")

# Group the data by cohort and calculate retention rate
cohort_group = df.groupby(["CohortMonth", "CohortIndex"])["UserId"].nunique()
cohort_data = cohort_group.unstack(0)
retention_matrix = cohort_data.divide(cohort_data.iloc[0], axis=1)

# Plot the data using a heatmap
plt.figure(figsize=(10, 8))
plt.title("Cohort Analysis - Retention")
sns.heatmap(data=retention_matrix, annot=True, fmt=".0%", cmap="YlGnBu")
plt.show()

This code assumes that the cohort_data.csv file contains columns for CohortMonth, CohortIndex, and UserId. The code groups the data by CohortMonth and CohortIndex, calculates the retention rate for each cohort, and plots the result using a heatmap.


The data that could be in cohort_data.csv:

UserId,CohortMonth,CohortIndex
1,January,1
2,January,1
3,January,1
4,January,2
5,January,2
6,February,1
7,February,1
8,March,1
9,March,2
10,March,2

The data contains information about users, the month they signed up (CohortMonth), and the cohort index (CohortIndex) which is the number of the cohort (e.g. the 1st cohort, the 2nd cohort, etc.). In this example, users 1-3 signed up in January and are part of the first cohort, users 4-5 signed up in January and are part of the second cohort, etc.


Use ReactJS to plot a heatmap using ECharts. Here’s a sample implementation:

import React, { useRef, useEffect } from 'react';
import echarts from 'echarts';

const Heatmap = () => {
  const chartRef = useRef(null);

  useEffect(() => {
    // Initialize the chart
    const myChart = echarts.init(chartRef.current);

    // Set chart option
    const option = {
      title: {
        text: 'Cohort Analysis - Retention',
        left: 'center',
      },
      tooltip: {
        formatter: '{a}<br />{b}: {c}%'
      },
      xAxis: {
        type: 'category',
        data: ['Cohort 1', 'Cohort 2', 'Cohort 3', ...],
      },
      yAxis: {
        type: 'category',
        data: ['Month 1', 'Month 2', 'Month 3', ...],
      },
      visualMap: {
        min: 0,
        max: 100,
        calculable: true,
        orient: 'horizontal',
        left: 'center',
        bottom: '15%'
      },
      series: [{
        name: 'Retention',
        type: 'heatmap',
        data: [
          [0, 0, 5], [0, 1, 8], [0, 2, 10], ...
        ],
        label: {
          show: true
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }]
    };

    // Apply the option
    myChart.setOption(option);

    // Re-render the chart when the window resizes
    window.addEventListener('resize', () => {
      myChart.resize();
    });

    return () => {
      window.removeEventListener('resize', () => {
        myChart.resize();
      });
    };
  }, []);

  return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
};

export default Heatmap;

This code uses the useRef and useEffect hooks from React to initialize and configure an ECharts chart, which is then rendered inside a div. The useEffect hook is used to ensure that the chart is re-rendered whenever the window resizes. You can then import and use this Heatmap component in your React application.

Leave a Reply

Your email address will not be published. Required fields are marked *