1

I cannot seem to grasp how to draw multiple bar histograms. This is my code:

import matplotlib.pyplot as mpl

tags = 'manual (4 serv.)', 'Cromlech (average, 4 serv.)', 'Cromlech (0.925, 4 serv.)', 'Cromlech (0.925 improved, 7 serv.)', 'Cromlech (15 serv.)', 'Pangaea', 'ServiceCutter (4 serv.)'
a = (0.385, 0.4128, 0.406, 0.5394, 0.7674, 0.306, 0.3505)
b = (0.4025, 0.1935, 0.189, 0.189, 0.415, 0.238, 0.1714)
c = (1, 0.3619, 0.5149, 1, 0.4851, 0.4092, 0.4407)
d = (1, 0.3495, 0.4888, 1, 0.4861, 0.4985, 0.5213)
mpl.hist((a, b, c, d), 7, label=("decoupling", "op. cost", "op. similarity", "data similarity"))
mpl.legend()
mpl.xticks((1, 2, 3, 4, 5, 6, 7), (tags))
mpl.show()

What I'm trying to do is produce a plot with 7 data points, each characterized by a quadruple (decoupling, op. cost, op. similarity, data similarity). "a, b, c, d" respectively contains the value for decoupling, op. costs... I want to label each of the data points with one of the tags in the code.

wrong plot

I don't understand what I'm doing wrong. Could you help me?

1 Answer 1

2

It looks like you want to create a bar plot, not a histogram.

In this case, the grouping, the labels and the legend are easiest if you create a pandas dataframe, and use pandas plotting (pandas uses matplotlib for plotting):

import matplotlib.pyplot as plt
import pandas as pd

tags = ('manual (4 serv.)', 'Cromlech (average, 4 serv.)', 'Cromlech (0.925, 4 serv.)',
        'Cromlech (0.925 improved, 7 serv.)', 'Cromlech (15 serv.)', 'Pangaea', 'ServiceCutter (4 serv.)')
# insert some newlines in the tags to better fit into the plot
tags = [tag.replace(' (', '\n(') for tag in tags]
a = (0.385, 0.4128, 0.406, 0.5394, 0.7674, 0.306, 0.3505)
b = (0.4025, 0.1935, 0.189, 0.189, 0.415, 0.238, 0.1714)
c = (1, 0.3619, 0.5149, 1, 0.4851, 0.4092, 0.4407)
d = (1, 0.3495, 0.4888, 1, 0.4861, 0.4985, 0.5213)
# create a dataframe
df = pd.DataFrame({"decoupling": a, "op. cost": b, "op. similarity": c, "data similarity": d}, index=tags)
df.plot.bar(rot=0, figsize=(12, 5))
plt.tight_layout() # fit labels etc. nicely into the plot
plt.show()

pandas bar plot

Optionally, you can modify visual aspects. Here is an example:

ax = df.plot.bar(rot=0, figsize=(12, 5), width=0.75, cmap='turbo')
for spine in ['top', 'right']:
    ax.spines[spine].set_visible(False)
ax.set_axisbelow(True)
ax.grid(axis='y', color='grey', ls=':')
ax.set_facecolor('beige')
ax.set_ylim(0, 1)
plt.tight_layout()

pandas barplot with changed visual aspect

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

1 Comment

Excellent!!! Yeah I'm not really into data viz so I didn't even know the name lol. BTW, thanks a lot!

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.