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

如何绘制多列数据的归一化计数图?编程语言年度占比可视化

How to Plot Yearly Programming Language Usage Ratios (Per Language)

Hey there! Let's get your language usage visualization sorted out—you're already halfway there with that grouped mean data, so we just need to reshape it properly and plot it.

Step 1: Reshape Your Data with melt() (No, You Won't Lose Your Ratios!)

Your worry about losing ratio data with melt is unfounded—this function only reshapes your data from wide format (one column per language) to long format (one row per year-language pair), keeping all your calculated ratios intact. Here's how to do it:

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

# Assuming df_tech is your precomputed ratio DataFrame
# First, reset the index to make 'year' a regular column
df_tech_reset = df_tech.reset_index()

# Reshape to long format
df_melted = df_tech_reset.melt(
    id_vars='year', 
    var_name='language', 
    value_name='usage_ratio'
)

This will give you a DataFrame with three columns: year, language, and usage_ratio (your 0-1 proportion values).

Step 2: Plot with Seaborn's barplot

Now you can use seaborn's barplot to create exactly the visualization you want: X-axis as languages, with separate bars for each year showing their usage ratio.

plt.figure(figsize=(10, 6))
# Use hue='year' to group bars by year for each language
sns.barplot(data=df_melted, x='language', y='usage_ratio', hue='year')

# Customize the plot for clarity
plt.title('Programming Language Usage by Year (Ratio)')
plt.ylabel('Usage Ratio')
plt.xlabel('Programming Language')
plt.ylim(0, 1)  # Since ratios are between 0 and 1

# Optional: Convert ratios to percentages for readability
# If you want percentages instead of raw ratios, uncomment these lines:
# df_melted['usage_ratio'] = df_melted['usage_ratio'] * 100
# plt.ylabel('Usage Percentage (%)')
# plt.ylim(0, 100)

plt.show()

This will produce a plot where each language has a set of bars (one per year) showing how its usage proportion changed over time—perfect for direct comparison.

Alternative: Pandas Built-in Plot (If You Prefer Year as X-Axis)

If you ever want to flip the perspective (X-axis as years, one bar per language per year), you can plot directly from your original df_tech without melting:

df_tech.plot(kind='bar', figsize=(10, 6))
plt.title('Language Usage Ratios by Year')
plt.ylabel('Usage Ratio')
plt.xlabel('Year')
plt.legend(title='Programming Language')
plt.show()

But this isn't the exact layout you asked for, so the melted + seaborn approach is your best bet.

Quick Check to Verify

Just to confirm your initial ratio calculation is correct: since your original data uses 0/1 indicators (1 = user uses the language), taking the mean per year does give you the percentage of users who used that language that year. So your df_tech is already correctly computed—great job on that part!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:53:19