You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python的Cufflinks绘制多系列气泡图并按大洲配色

Got it, let's build that multi-series bubble chart you're after—with each continent in a custom color, using the Gapminder dataset. Here's a complete, step-by-step breakdown:

Step 1: Set Up Dependencies

First, make sure you have the required libraries installed and imported. We'll use pandas for data handling and cufflinks for plotting:

import pandas as pd
import cufflinks as cf

# Optional: Enable offline mode to avoid relying on Plotly's online servers
cf.go_offline()

Step 2: Load & Prep the Gapminder Data

We'll load the dataset, then filter to a single year (2007 works great for a clear snapshot—you can adjust this if you want a different year):

# Load the dataset
df = pd.read_csv('http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt', sep='\t')

# Filter to 2007 to get a static snapshot (remove this line if you want all years)
df = df[df['year'] == 2007]

Step 3: Define Custom Continent Colors

Map each continent to a specific color of your choice. Feel free to tweak these hex codes to match your preferred palette:

continent_color_map = {
    'Asia': '#FF6B6B',       # Coral red
    'Europe': '#4ECDC4',     # Teal
    'Africa': '#45B7D1',     # Bright blue
    'Americas': '#96CEB4',   # Mint green
    'Oceania': '#FFEAA7'     # Pale yellow
}

Step 4: Generate the Multi-Series Bubble Chart

Use Cufflinks' iplot() method, with key parameters to group by continent, apply custom colors, and size bubbles by population:

df.iplot(
    x='gdpPercap',
    y='lifeExp',
    mode='markers',
    categories='continent',  # Groups data into series by continent
    marker=dict(
        # Assign custom color to each row based on its continent
        color=[continent_color_map[cont] for cont in df['continent']],
        # Scale population by 1e6 to make bubble sizes manageable
        size=df['pop'] / 1_000_000,
        sizemode='area'  # Ensures bubble area scales proportionally to population
    ),
    xTitle='GDP per Capita (USD)',
    yTitle='Life Expectancy (Years)',
    title='2007 Gapminder: Life Expectancy vs. GDP per Capita',
    text='country',  # Shows country name on hover
    hoverinfo='text+x+y+size'  # Customize hover tooltip content
)

Key Notes:

  • The categories='continent' parameter is what creates the multi-series grouping—each continent will appear as a separate series in the legend.
  • We scale the population by 1e6 because raw population values are huge, which would make bubbles overlap excessively.
  • The text='country' parameter adds context to each bubble, so users can hover to see which country each point represents.

This will produce a chart just like the example you referenced, with each continent in your specified colors, bubbles sized by population, and clear hover details.

内容的提问来源于stack exchange,提问作者Sharvari Gc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:52:47