0

Using Matplotlib, I display a dictionary chart. But I want to have the features in the vertical axis. This does not happen by changing the order of the first and second plt.bar parameters.

my_dictionary = {'F1': 0.0, 'F2': -0.03137518153387725, 'F3': -0.028483522708153908, 'F4': -0.04285443514756063, 'F5': 0.007765016154692321, 'F6': 0.003007286684430399, 'F7': 0.0033222336382515774, 'F8': 0.0013582396102663208, 'F9': -0.0025918156002548154, 'F10': 0.0202258302015568, 'F11': 0.030016293782106906, 'F12': 0.000827900819205335, 'F13': -0.010119661798570101, 'F14': -0.014028783493522351, 'F15': -0.0015247180908278396, 'F16': -0.004395984305092304, 'F17': 0.005053514422172792}

flag1 = 0
flag2 = 0
for i in my_dictionary.items():
    if i[1] <= -0.002:
        if flag1 == 0:
            plt.bar(i[0], i[1], width=0.8, color='orange', label='L')
            flag1 = 1
        else:
            plt.bar(i[0], i[1], width=0.8, color='orange')

    elif i[1] >= 0.002:
        if flag2 == 0:
            plt.bar(i[0], i[1], width=0.8, color='purple', label='H')
            flag2 = 1
        else:
            plt.bar(i[0], i[1], width=0.8, color='purple')

plt.grid()
plt.xticks(rotation='vertical')
plt.legend()
plt.show()
Flodude
  • 255
  • 2
  • 12
  • I also used the flags to prevent the duplication of chart legend! Which I'm not sure is the best way but it works ... – Flodude Dec 26 '20 at 12:24
  • 1
    Please see the matplotlib documentation for horizontal bars or [this example on their website.](https://matplotlib.org/3.1.3/gallery/lines_bars_and_markers/barh.html) – Mr. T Dec 26 '20 at 12:27
  • 1
    This question also annotates but basically repeats what the matplotlib website suggests: [How to display the value of the bar on each bar with pyplot.barh()?](https://stackoverflow.com/questions/30228069/how-to-display-the-value-of-the-bar-on-each-bar-with-pyplot-barh) – Mr. T Dec 26 '20 at 12:29

0 Answers0