如何在条形图上添加一条线?

时间:2016-06-24 15:55:01

标签: python pandas matplotlib

我有一个条形图,我想在其上绘制两个坐标之间的线段(即,在同一图中)。我尝试添加另一个plt语句,但是没有绘制该行。

InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

2 个答案:

答案 0 :(得分:3)

<强>更新

$(document).ready(function () {
    $('input[type="radio"]').change(function () {
        if ($(this).val()==true) {
            $('#ImportPartQuestions').show();
        }

       else {
           $('#ImportPartQuestions').hide();
       }
   });
});

enter image description here

OLD回答:

这是一个小型演示:

import datetime
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.style.use('ggplot')

d = {
    datetime.date(2015, 6, 21): 101.0,
    datetime.date(2015, 6, 22): 81.0, 
    datetime.date(2015, 6, 23): 94.0, 
    datetime.date(2015, 4, 24): 67.5, 
    datetime.date(2015, 6, 26): 99.1
}

df = pd.DataFrame({'date': [x for x in d.keys()], 'val':[x for x in d.values()]})

%matplotlib

ax = df.set_index('date').plot.bar(rot=0, figsize=(12, 10))

x = [ax.patches[0].get_x(), ax.patches[-1].get_x() + ax.patches[-1].get_width()]
y = [df.val.max()] * 2

plt.plot(x, y, 'r--', c='g', linewidth=4)

#plt.plot([ax.patches[0].get_width(), ax.patches[-1].get_width()], [y,y], 'r--', c='k')

enter image description here

答案 1 :(得分:0)

使用matplotlib 3.1.0可以轻松得多。您要做的就是在show()之前调用hlines()方法进行绘图:

freedom = np.array([1., 2., 3., 4.])
acc_one = [95.013, 95.039, 94.995, 94.952]
acc_two = [94.987, 95.013, 94.969, 94.926]
acc_three = [95.004, 95.021, 94.978, 94.909]
acc_four = [95.004, 95.013, 94.969, 94.917]

ax = plt.subplot(111)
rec1 = ax.bar(freedom-0.4, acc_one,  width=0.2, color='b', align='center')
rec2 = ax.bar(freedom-0.2, acc_two,  width=0.2, color='m', align='center')
rec3 = ax.bar(freedom, acc_three, width=0.2,color='g', align='center')
rec4 = ax.bar(freedom+0.2, acc_four, width=0.2,color='r', align='center')

ax.set_ylim([94.5, 95.2])
ax.set_xticks(freedom)
ax.set_ylabel('Accuracy')
ax.set_xticklabels(('f=1', 'f=2', 'f=3', 'f=4'))
ax.set_xlabel('Degree of freedom')
ax.legend((rec1, rec2, rec3, rec4), ('A', 'B', 'C', 'D'))
plt.hlines(95.013, xmin=0, xmax=5, linestyles='dashed')
plt.show()

enter image description here

here中提供了参数的完整说明。