1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import pandas as pd
import matplotlib.pyplot as plt
# Import data
data = pd.read_table('G:/work/essai2.txt', sep='\t')
# Reverse table from smallest to highest
data2 = data.sort_index(by=['y', 'x'], ascending=[True, False])
print(data2)
tickers = data2['x'].values
# Plot data
fig, ax = plt.subplots(figsize=(10,11))
rects = ax.barh(range(len(tickers)), data2['y'].values, align='center', color='#4E8DC4', alpha=0.6)
ax.set_yticks(range(len(tickers)))
ax.set_yticklabels(tickers)
ax.set_xlabel('Hmm')
ax.set_title('title')
ax.set_ylim((-1, len(tickers))) # allows to auto-size Y axis depending of [-1:N] bars
ax.grid()
# Rotating the xticklabels for patents volume
for label in ax.get_xmajorticklabels():
label.set_rotation(30)
label.set_verticalalignment("top")
# Add label to the right of the bars
for i, rect in enumerate(rects):
# Get width of the bar
xloc = int(rect.get_width()) + 35 #x-dimension label position (the highest, the farthest)
# Center the text vertically in the bar
yloc = rect.get_y() + rect.get_height()/3.0 #y-dimension label position
label = ax.text(
xloc, yloc,
data2['y'].values[i],
horizontalalignment='right',
verticalalignment='center',
color='k',
clip_on=True,
)
plt.tight_layout() # allows to autosize-labels & tickers to fit diagram
plt.show() |
Partager