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

如何实现带气泡的折线图(样条曲线)?能否将气泡图X轴设为时间?

Can I Create a Spline Line Chart with Bubbles (Time X-axis, Numeric Y/Z Axes)?

Absolutely feasible! You can absolutely build a spline line chart with bubbles where your X-axis uses a time dimension, Y-axis represents numerical values, and bubble size maps to another numerical variable (your "Z-axis" equivalent). Most modern data visualization libraries support this natively—let’s walk through how to implement it with two popular tools:

1. Plotly (Interactive, Easy Setup)

Plotly is perfect for this because it handles time axes seamlessly and lets you combine chart types with minimal code. Here’s a working example:

import plotly.express as px
import pandas as pd

# Sample dataset with time, Y value, and bubble size metrics
df = pd.DataFrame({
    "date": pd.date_range(start="2024-01-01", periods=10, freq="D"),
    "daily_value": [12, 15, 13, 18, 20, 17, 22, 25, 23, 28],
    "transaction_volume": [30, 40, 25, 50, 55, 45, 60, 70, 65, 75]
})

# Create scatter plot with bubbles, then add a spline line
fig = px.scatter(df, x="date", y="daily_value", size="transaction_volume", 
                 title="Daily Value Trend with Transaction Volume Bubbles")
# Add spline line trace
fig.add_scatter(x=df["date"], y=df["daily_value"], mode="lines", 
                line=dict(shape="spline", color="darkblue"), name="Trend Line")

# Format axes
fig.update_layout(xaxis_title="Date", yaxis_title="Daily Value")
fig.show()
  • Plotly automatically recognizes the date column as a time dimension, so your X-axis will have proper date formatting, zoom, and pan controls.
  • The size parameter directly maps your "Z-axis" numeric values to bubble size.
  • The spline line is added as a separate trace with shape="spline" for smooth curves.

2. Matplotlib (Static, Highly Customizable)

If you prefer static charts, Matplotlib can also handle this with a bit of extra setup for the spline:

import matplotlib.pyplot as plt
import pandas as pd
from scipy.interpolate import make_interp_spline
import numpy as np

# Sample data
df = pd.DataFrame({
    "date": pd.date_range(start="2024-01-01", periods=10, freq="D"),
    "daily_value": [12, 15, 13, 18, 20, 17, 22, 25, 23, 28],
    "transaction_volume": [300, 400, 250, 500, 550, 450, 600, 700, 650, 750]
})

# Convert dates to numeric indices for spline interpolation
x_indices = np.arange(len(df["date"]))
# Create smooth spline curve
spline = make_interp_spline(x_indices, df["daily_value"], k=3)
x_smooth = np.linspace(x_indices.min(), x_indices.max(), 500)
y_smooth = spline(x_smooth)

# Plotting
plt.figure(figsize=(10, 6))
# Draw spline line
plt.plot(x_smooth, y_smooth, color="darkblue", label="Smooth Trend")
# Draw bubbles (scatter with size tied to transaction volume)
plt.scatter(x_indices, df["daily_value"], s=df["transaction_volume"], 
            color="orange", alpha=0.6, label="Daily Data")

# Set X-axis to show dates
plt.xticks(x_indices, df["date"].dt.strftime("%Y-%m-%d"), rotation=45)
plt.xlabel("Date")
plt.ylabel("Daily Value")
plt.title("Daily Value Trend with Transaction Volume Bubbles")
plt.legend()
plt.tight_layout()
plt.show()
  • We use numeric indices to generate the spline curve, then map those indices back to your original date labels for the X-axis.
  • Bubble size is controlled via the s parameter in plt.scatter() (note: values here are scaled up for visibility).

Quick Tips:

  • Time Axis Compatibility: Don’t worry about the "only numbers/literals" limitation—all major libraries support datetime objects or date string formatting for X-axes.
  • Spline Smoothness: Adjust the k parameter in make_interp_spline (Matplotlib) or frac in Plotly’s trendline to control how smooth your curve is.
  • Color Customization: You can add another dimension by mapping bubble color to a third variable if needed.

内容的提问来源于stack exchange,提问作者Rohit Sharma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:54:15