Reputation: 429
I want to create horizontal bar chart using python, but there are too many categories like this
import matplotlib.pyplot as plt
x = ['c0', 'c1', 'c2', 'c3', 'c4',
'c5', 'c6', 'c7', 'c8', 'c9',
'c10', 'c11', 'c12', 'c13', 'c14',
'c15', 'c16', 'c17', 'c18', 'c19',
'c20', 'c21', 'c22', 'c23', 'c24',
'c25', 'c26', 'c27', 'c28', 'c29']
y = [9556, 9318, 9308, 2556, 2424, 1812, 1624, 1468, 1398, 1344, 1200,
1056, 888, 732, 732, 704, 684, 680, 666, 660, 636, 624,
576, 552, 544, 532, 468, 448, 448, 436]
plt.barh(new_x, new_y)
The above code is only a part of the categories, in fact, the number of my categories is more than 200.
I want to cut into two subplots to present,
like this
Is there any way to plot the above graph in python?
Upvotes: 0
Views: 256
Reputation: 337
Use an index to split your data, and a vertical subplot to plot you data. E.g. something like this:
split = len(x) // 2
fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=False, figsize=(10, 8))
x = ['c0', 'c1', 'c2', 'c3', 'c4',
'c5', 'c6', 'c7', 'c8', 'c9',
'c10', 'c11', 'c12', 'c13', 'c14',
'c15', 'c16', 'c17', 'c18', 'c19',
'c20', 'c21', 'c22', 'c23', 'c24',
'c25', 'c26', 'c27', 'c28', 'c29']
y = [9556, 9318, 9308, 2556, 2424, 1812, 1624, 1468, 1398, 1344, 1200,
1056, 888, 732, 732, 704, 684, 680, 666, 660, 636, 624,
576, 552, 544, 532, 468, 448, 448, 436]
ax1.barh(x[0:split], y[0:split])
ax2.barh(x[split:], y[split:])
Tweak the plot further to get your plots the same as your example.
Upvotes: 1