Complete Guide to Plotting Multiple Lines with Different Colors Using pandas DataFrame

Nov 23, 2025 · Programming · 13 views · 7.8

Keywords: pandas | data_visualization | multiple_line_plotting | color_mapping | pivot_table

Abstract: This article provides a comprehensive guide to plotting multiple lines with distinct colors using pandas DataFrame. It analyzes three technical approaches: pivot table method, group iteration method, and seaborn library method, delving into their implementation principles, applicable scenarios, and performance characteristics. The focus is on explaining the data reshaping mechanism of pivot function and matplotlib color mapping principles, with complete code examples and best practice recommendations.

Data Visualization Requirements Analysis

In data analysis and scientific computing, it is often necessary to plot data from different categories in the same dataset with distinct colors for comparative analysis. pandas, as a powerful data processing library in Python, provides built-in plotting capabilities based on matplotlib, offering convenient data visualization interfaces.

Detailed Explanation of Pivot Table Method

The pandas.DataFrame.pivot function can transform long-format data into wide-format, which is the core method for plotting multiple lines. This function reorganizes the data structure by specifying index columns, column names, and value columns, making each category an independent column.

The following code demonstrates the complete data pivoting and plotting process:

import pandas as pd
import matplotlib.pyplot as plt

# Create sample data
data = [
    ["red", 0, 0], ["red", 1, 1], ["red", 2, 2], ["red", 3, 3],
    ["red", 4, 4], ["red", 5, 5], ["red", 6, 6], ["red", 7, 7],
    ["red", 8, 8], ["red", 9, 9], ["blue", 0, 0], ["blue", 1, 1],
    ["blue", 2, 4], ["blue", 3, 9], ["blue", 4, 16], ["blue", 5, 25],
    ["blue", 6, 36], ["blue", 7, 49], ["blue", 8, 64], ["blue", 9, 81]
]
df = pd.DataFrame(data, columns=["color", "x", "y"])

# Data pivoting process
df_pivot = df.pivot(index="x", columns="color", values="y")

# Plot multiple lines
df_pivot.plot(color=df_pivot.columns, figsize=(8, 6), linewidth=2)
plt.title("Multi-color Line Comparison Chart")
plt.xlabel("X-axis Coordinate")
plt.ylabel("Y-axis Value")
plt.grid(True, alpha=0.3)
plt.show()

Color Mapping Mechanism Analysis

In matplotlib, colors can be specified in various ways. When using the DataFrame.plot method, the color parameter can accept a list of color names. pandas automatically matches column names with matplotlib-supported color names. If column names happen to be valid color names, the system directly uses the corresponding colors for plotting.

For non-standard color names, explicit color mapping can be specified:

# Explicit color mapping example
color_mapping = {"red": "#FF0000", "blue": "#0000FF"}
colors = [color_mapping[col] for col in df_pivot.columns]
df_pivot.plot(color=colors, figsize=(8, 6))

Alternative Methods Comparison

Besides the pivot table method, other viable technical approaches exist. The group iteration method uses DataFrame.groupby function to group by color and then plot each subset individually:

fig, ax = plt.subplots(figsize=(8, 6))

for color_name, group_data in df.groupby("color"):
    group_data.plot(x="x", y="y", ax=ax, label=color_name, color=color_name)

plt.legend()
plt.show()

The seaborn library provides more concise syntax, particularly suitable for statistical visualization:

import seaborn as sns

sns.lineplot(data=df, x="x", y="y", hue="color", palette="deep")
plt.show()

Performance and Applicability Analysis

The pivot table method exhibits good performance when handling large datasets, as it performs data reorganization only once in memory. The group iteration method, while having clear code logic, may encounter performance bottlenecks when processing numerous groups. The seaborn method offers concise syntax suitable for rapid prototyping but has relatively limited customization capabilities.

Best Practice Recommendations

In practical applications, it is recommended to choose the appropriate method based on data scale and specific requirements. For well-structured data, the pivot table method is the optimal choice. When highly customized visual effects are needed, the group iteration method provides greater flexibility. During exploratory data analysis, seaborn's concise syntax can significantly improve work efficiency.

Regardless of the method chosen, attention should be paid to the importance of data preprocessing. Ensure that categorical variable values conform to matplotlib color specifications or establish clear color mapping relationships in advance. This approach helps avoid runtime errors and achieve expected visualization results.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.