創建分級標籤要爲這樣的barplot創建matplotlib分級標籤:我如何在matplotlib
兩個競爭對手在5次比較。我想獲得類似於
Competitor1 Competitor2 Competitor1 Competitor2
1 2
等
標籤是否有可能在matplotlib或我必須手動添加後呢?
感謝
我的代碼:
# imports
import pandas as pd
import sys
import itertools
import os
import matplotlib as mpl
from matplotlib import pyplot as plt
import numpy as np
# axes limits
x_min = 1
y_min = 0
x_max = None
y_max = None
bar_width = 0.4
bar_offset = -bar_width
for method in methods:
# load method's data
data = pd.read_table(fpath, sep='\t', index_col=0)
# update x position with offset
x = data.index + bar_offset
# grey is always higher than black
y_grey = data['grey']
y_black = data['black']
# update x-axis upper limit
x_max = max(x) if x_max is None else max(max(x), x_max)
# update y-axis upper limit
if y_max is None:
y_max = max(y_grey + y_grey)
else:
y_max = max(max(y_grey + y_grey), y_max)
# plot grey and black bars
color = 'k'
plt.bar(x, y_grey, color=color, alpha=0.65, width=bar_width)
plt.bar(x, y_black, color=color, alpha=1, width=bar_width)
# update offset
bar_offset += bar_width + 0.01
# labels and limits
xlabel = 'Classes'
plt.xlabel(xlabel)
plt.xlim(x_min - 0.6, x_max + 0.6)
plt.ylim(y_min - 1, y_max + 1)
# squared aspect
ax = plt.axes()
aspect = np.diff(ax.get_xlim())/np.diff(ax.get_ylim())
ax.set_aspect(aspect)
# save figure
plt.savefig(output_fn)
plt.close()
我會建議添加相關的繪圖代碼。 – mehmetminanc
Acc,我忘了。這裏是.. – gc5