我想显示一个条形图,比较不同出版物的算法。
数据具有以下属性:
我无法实现这一目标(避之不及强制执行第3项要求)。
以下是一个示例数据和代码:
import numpy as np
import matplotlib.pyplot as plt
dtypes = ['type1', 'type2', 'type3']
names = ['name1','name2','name3', 'name4']
score = [89.2, 95.54, 85, 86]
years = [2016, 2017, 2016, 2015]
methods_dtype = ['type1', 'type2', 'type1', 'type1']
pub_years = np.unique(years)
fig, ax = plt.subplots()
barplot = ax.bar(years, score)
plt.show()
这里的第一个问题是2016年的两个条形图是彼此重叠的(我看到一些使用宽度逐渐移动条形的示例,但是,在这种情况下,我事先不知道有多少方法在那一年) 第二个问题是编码颜色。
注意输入只是数据的一个子集。可能有一年有多个条目(特定年份的多个出版物)。可能还有一个包含多个条目的数据类型(多个方法对此数据类型进行操作)。
答案 0 :(得分:2)
以下是您可以做的一个示例,由您根据您的具体需求进行调整:
score = range(1,7)
years = [2015, 2016, 2017]*2
methods_dtype = ['type1', 'type2']*3
color = {'type1': 'b', 'type2': 'g'}
offset = {'type1': -0.2, 'type2': 0}
plt.figure(1).clf()
for s, y, m in zip(score, years, methods_dtype):
x = y + offset[m]
plt.bar(x, s, color=color[m], width=0.2)
plt.xticks([2015, 2016, 2017], [2015, 2016, 2017])
答案 1 :(得分:0)
所以我最终解决了这个问题,并希望发布解决方案以供其他人参考:
它的灵感来自朱利安的答案。
我所做的是分别绘制每个条形图,同时使用2D数组和数据类型跟踪间距。我也做得更漂亮。
adb devices