1

I have the code for following graph. I need to add a threshold value for each of the graph. This value does change from one group to another.

import numpy as np
import matplotlib.pyplot as plt

n_groups = 4
heuristic = (230.193, 33.96, 46, 8)
safe = (195.8, 24.83, 36, 7)

# create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.1
opacity = 0.8

rects1 = plt.bar(index, heuristic, bar_width,
             marker="D",
             alpha=opacity,
             color='b',
             label='Heuristic')

rects2 = plt.bar(index + bar_width, safe, bar_width,
             alpha=opacity,
             color='g',
             label='SAFE')

plt.xlabel('Firmware')
plt.ylabel('nDCG')
plt.title('Matching results by firmware')
plt.xticks(index + bar_width, ('Mqtt', 'Solder', 'Iron', 'Sympetrum'))
plt.legend()

plt.tight_layout()
plt.show()

The following is the graph it produces and I have used pink marker to show you what I want the code to produce. Sorry the lines are not straight. Any help is really appreciated. Thanks

The wanted graph

1 Answer 1

1

Here's a way using hlines:

import numpy as np
import matplotlib.pyplot as plt

n_groups = 4
heuristic = (230.193, 33.96, 46, 8)
safe = (195.8, 24.83, 36, 7)
threshold = (250, 50, 80, 30)

# create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.1
opacity = 0.8

rects1 = plt.bar(index, heuristic, bar_width,
             #marker="D",
             alpha=opacity,
             color='b',
             label='Heuristic')

rects2 = plt.bar(index + bar_width, safe, bar_width,
             alpha=opacity,
             color='g',
             label='SAFE')

[
    ax.hlines(
        threshold[i],
        index[i] - bar_width / 2,
        index[i] + bar_width * 1.5,
        colors="deeppink",
    )
    for i in range(len(safe))
]

plt.xlabel('Firmware')
plt.ylabel('nDCG')
plt.title('Matching results by firmware')
plt.xticks(index + bar_width, ('Mqtt', 'Solder', 'Iron', 'Sympetrum'))
plt.legend()

plt.tight_layout()
plt.show()

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.