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

基于RFMClass统计客户数量并绘制直方图的Python技术问询

Alright, since you’ve already imported your customer list into Python for RFM analysis and added the RFMClass field to your dataset, let’s walk through how to create a clear histogram that shows how many customers fall into each RFM class. I’ll cover two common Python visualization approaches—Matplotlib for basic control and Seaborn for more polished out-of-the-box plots.

Plotting Customer Count by RFM Class

Assuming your data is stored in a pandas DataFrame (standard for this kind of analysis in Python), here are step-by-step implementations:

Method 1: Basic Histogram with Matplotlib

This gives you full control over every element of the plot:

  1. First, import the required libraries:
import pandas as pd
import matplotlib.pyplot as plt
  1. Let’s assume your DataFrame is named rfm_data. We’ll first count customers per RFM class, then plot:
# Calculate customer counts for each RFM class, sorted by class number
rfm_class_counts = rfm_data['RFMClass'].value_counts().sort_index()

# Create the histogram (bar plot, since RFMClass is categorical)
plt.figure(figsize=(10, 6))
rfm_class_counts.plot(kind='bar', color='slateblue')

# Add labels and styling for clarity
plt.title('Customer Distribution by RFM Class', fontsize=14)
plt.xlabel('RFM Class', fontsize=12)
plt.ylabel('Number of Customers', fontsize=12)
plt.xticks(rotation=0)  # Keep x-axis labels horizontal for readability
plt.grid(axis='y', linestyle='--', alpha=0.6)

# Optional: Add value labels on top of each bar
for index, count in enumerate(rfm_class_counts):
    plt.text(index, count + 2, str(count), ha='center', va='bottom')

plt.show()

Method 2: Polished Count Plot with Seaborn

Seaborn simplifies creating aesthetically pleasing statistical plots, great for quick, clean visualizations:

  1. Import the necessary libraries:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
  1. Use countplot to directly plot customer counts per RFM class:
plt.figure(figsize=(10, 6))

# Plot counts, ensuring RFM classes are sorted numerically
sns.countplot(
    data=rfm_data,
    x='RFMClass',
    order=sorted(rfm_data['RFMClass'].unique()),
    palette='coolwarm'
)

# Add labels and styling
plt.title('Customer Distribution by RFM Class', fontsize=14)
plt.xlabel('RFM Class', fontsize=12)
plt.ylabel('Customer Count', fontsize=12)
plt.grid(axis='y', linestyle='--', alpha=0.6)

# Optional: Add value labels
for bar in plt.gca().patches:
    height = bar.get_height()
    plt.text(
        bar.get_x() + bar.get_width()/2,
        height + 2,
        f'{height}',
        ha='center', va='bottom'
    )

plt.show()

Quick Notes

  • If your RFMClass is stored as a string (instead of integer), convert it to numeric before sorting to avoid odd ordering (e.g., "100" appearing before "20"):
    rfm_data['RFMClass'] = pd.to_numeric(rfm_data['RFMClass'])
    
  • Adjust the figsize, color, and palette parameters to match your preferred style or reporting needs.

内容的提问来源于stack exchange,提问作者Ben P

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:20:50