Skip to content
Advertisement

Sorted X axis ticks in Matplotlib when using multiple datasets

I have several datasets that I want to plot. The x values are “categorical” strings, in the sense that they have no particularly meaning, but they are sortable (alphanumeric). Each dataset has some X values that are unique, and some that are in common. The desired output is a graph where the xXtick labels are in sorted order.

Unfortunately I can’t find a way to tell matplotlib what I want. It properly combines the Y values on the common X values, but puts the unique X values in the later datasets on the right side of the graph.

Here’s an example to illustrate the issue:

import matplotlib.pyplot as plt
import numpy as np

# Create first data set
x1 = np.array(['a', 'c', 'e'])
y1 = np.array([1, 2, 5])

# Create second data set with some x values in common with first dataset
x2 = np.array(['a', 'b', 'd', 'e'])
y2 = np.array([2, 4, 1, 1])

# Plot both datasets
fig, ax = plt.subplots()
ax.plot(x1, y1, color='r', marker='+', linestyle='None')
ax.plot(x2, y2, color='b', marker='o', linestyle='None')

This results in the graph below.

enter image description here

The values for “a” and “e” are properly combined for both datasets. But I’d like to have “b” and “d” show up in sorted order, such that the X tick labels are “a”, “b”, “c”, “d”, “e”. I can produce a combined sorted set of X values using

sorted_x = np.sort(np.unique(np.hstack([x1, x2]))))

But I’m unsure how to leverage this to tell matplotlib this is the desired order.

Advertisement

Answer

You could first add a dummy plot using the x-values in the desired order. The following code creates the dummy plot, setting the x-values in the desired order. The plot is removed immediately to prevent that it interferes with other matplotlib functions.

import matplotlib.pyplot as plt
import numpy as np

# Create first data set
x1 = np.array(['a', 'c', 'e'])
y1 = np.array([1, 2, 5])

# Create second data set with some x values in common with first dataset
x2 = np.array(['a', 'b', 'd', 'e'])
y2 = np.array([2, 4, 1, 1])

# Plot both datasets
fig, ax = plt.subplots()

sorted_x = np.sort(np.unique(np.hstack([x1, x2])))

sorted_x = np.sort(np.unique(np.hstack([x1, x2])))
# create a dummy plot with the x-values in the desired order, and then remove the plot
ax.plot(sorted_x, np.full(len(sorted_x), np.NaN), color='none')[0].remove()
ax.plot(x1, y1, color='r', marker='+', linestyle='None')
ax.plot(x2, y2, color='b', marker='o', linestyle='None')
plt.show()

categorical x-axis sorted

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement