How to set Dataframe Column value as X-axis labels in Python Pandas?

Setting DataFrame column values as X-axis labels in Python Pandas can be achieved using the xticks parameter in the plot() method. This allows you to customize the X-axis labels to display specific column values instead of default indices.

Basic Example

Let's start with a simple example using a DataFrame with one column ?

import pandas as pd
import matplotlib.pyplot as plt

# Set figure size for better visualization
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create a DataFrame
data = pd.DataFrame({"values": [4, 6, 7, 1, 8]})
print("DataFrame:")
print(data)

# Plot with custom X-axis labels
data.plot(xticks=data.values.flatten())
plt.title("DataFrame Plot with Custom X-axis Labels")
plt.show()
DataFrame:
   values
0       4
1       6
2       7
3       1
4       8

Using Multiple Columns

You can also set X-axis labels using values from a different column ?

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [8.00, 4.00]

# Create DataFrame with multiple columns
data = pd.DataFrame({
    "months": ["Jan", "Feb", "Mar", "Apr", "May"],
    "sales": [120, 135, 148, 162, 180]
})

print("DataFrame:")
print(data)

# Plot sales with months as X-axis labels
data.plot(x="months", y="sales", kind="line", marker="o")
plt.title("Sales by Month")
plt.xlabel("Months")
plt.ylabel("Sales")
plt.show()
DataFrame:
  months  sales
0    Jan    120
1    Feb    135
2    Mar    148
3    Apr    162
4    May    180

Using xticks with Custom Values

For more control over X-axis labels, you can use the xticks parameter directly ?

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [8.00, 4.00]

# Create DataFrame
data = pd.DataFrame({
    "categories": ["A", "B", "C", "D", "E"],
    "scores": [85, 92, 78, 96, 88]
})

print("DataFrame:")
print(data)

# Plot with categories as X-axis labels using xticks
ax = data["scores"].plot(kind="bar", color="skyblue")
ax.set_xticklabels(data["categories"], rotation=0)
plt.title("Scores by Category")
plt.xlabel("Categories")
plt.ylabel("Scores")
plt.show()
DataFrame:
  categories  scores
0          A      85
1          B      92
2          C      78
3          D      96
4          E      88

Key Methods Summary

Method Use Case Best For
plot(x="col", y="col") Direct column mapping Simple X-Y plots
plot(xticks=values) Custom tick positions Specific label control
set_xticklabels() Post-plot customization Complex formatting

Conclusion

Use the x parameter in plot() for direct column mapping to X-axis. For custom control over tick positions and labels, combine xticks parameter with set_xticklabels() method.

Updated on: 2026-03-25T21:22:50+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements