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

如何为Pandas绘图标签添加LaTeX文本并精准控制颜色?

Solution: Custom Legend Labels with LaTeX and Color Control

When working with pandas df.plot() and needing to add transparent LaTeX text above each column name in the legend (with column names matching their line colors), you can use a custom matplotlib legend handler to achieve this without plotting each line individually. Here's how to do it efficiently, even for multiple DataFrames:

Step-by-Step Implementation

First, let's set up the necessary imports and sample DataFrame:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerBase
from matplotlib.text import Text

# Optional: Enable LaTeX rendering for better math text appearance (requires LaTeX installed)
plt.rcParams['text.usetex'] = True

# Sample DataFrame
df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['a', 'b', 'c'])

Custom Legend Handler

We'll create a handler that draws two text elements per legend entry:

  1. Transparent LaTeX text ($V_{sd}$) above the column name
  2. The column name colored to match its corresponding line
class CustomLegendHandler(HandlerBase):
    def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans):
        # LaTeX text: positioned above the column name, fully transparent
        latex_label = Text(
            xdescent + width/2,  # Horizontal center
            ydescent + height + fontsize/2,  # Position above the column name
            r'$V_{sd}$',
            ha='center', va='bottom',
            fontsize=fontsize,
            alpha=0  # Set to 0 for full transparency; adjust to 0.3 for semi-transparent
        )
        
        # Column name: colored to match the line, centered vertically
        column_label = Text(
            xdescent + width/2,
            ydescent + height/2,
            orig_handle.get_label(),  # Get the original column name
            ha='center', va='center',
            fontsize=fontsize,
            color=orig_handle.get_color()  # Match the line's color
        )
        
        return [latex_label, column_label]

Plot with Custom Legend

Now, plot your DataFrame and replace the default legend with our custom one:

# Plot the DataFrame
ax = df.plot()

# Update the legend using our custom handler
ax.legend(
    handles=ax.get_lines(),  # Use the existing line objects from the plot
    handler_map={plt.Line2D: CustomLegendHandler()}  # Apply our handler to all Line2D elements
)

plt.show()

Reusable Function for Multiple DataFrames

To avoid repeating code for many DataFrames, wrap the logic in a function:

def plot_with_latex_labels(df, latex_text=r'$V_{sd}$', latex_alpha=0):
    """
    Plot a DataFrame with custom legend labels (LaTeX text above column names).
    
    Args:
        df: pandas DataFrame to plot
        latex_text: LaTeX string to add above column names
        latex_alpha: Transparency of the LaTeX text (0 = fully transparent)
    
    Returns:
        matplotlib Axes object
    """
    ax = df.plot()
    
    class CustomLegendHandler(HandlerBase):
        def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans):
            latex_label = Text(
                xdescent + width/2,
                ydescent + height + fontsize/2,
                latex_text,
                ha='center', va='bottom',
                fontsize=fontsize,
                alpha=latex_alpha
            )
            column_label = Text(
                xdescent + width/2,
                ydescent + height/2,
                orig_handle.get_label(),
                ha='center', va='center',
                fontsize=fontsize,
                color=orig_handle.get_color()
            )
            return [latex_label, column_label]
    
    ax.legend(handles=ax.get_lines(), handler_map={plt.Line2D: CustomLegendHandler()})
    return ax

# Usage example for any DataFrame
ax = plot_with_latex_labels(df)
plt.show()

Key Notes

  • LaTeX Support: If you don't have LaTeX installed, matplotlib's built-in mathtext will still render $V_{sd}$ correctly (just remove the plt.rcParams['text.usetex'] = True line).
  • Transparency Adjustment: Change alpha=0 to a value between 0 and 1 if you want the LaTeX text to be semi-transparent instead of fully invisible.
  • Color Matching: The column name color automatically matches the line color from pandas' default plotting style (blue for first column, orange for second, etc.).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 15:57:36