My goal is to get a bar chart showing only the top edge of each bar, like the following:
I am using matplotlib in python to achieve this, the code I have is as follows:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_title("Punto 1 - Fr",fontsize = 20)
ax.set_xlabel("Demanda (X)",fontsize = 10)
ax.set_ylabel("Frecuencia",fontsize = 10)
ejeX = ["7","12","18","23","25","28","30"]
altura_barras = [0.23,0.4,0.6,0.77,0.84,0.94,1.01]
ax.bar(ejeX, altura_barras,width=0.2,color = "white",edgecolor = "green")
plt.show()
Which generates the following bar chart:
I am not able to get it, how could I solve it?
A somewhat tricky but very simple and quick solution is to make a diagram of "stacked" bars, in which there is not really the first "stack", only the second, and all the bars of the second stack have the same height, very small.
In short, it is about passing your list of data in the argument
bottom
, which indicates the height at which each bar begins, and passing it asheight
a default value of 0. In order for these "bars" to be visible, you will have to give a color and a thickness to its edges, usingedgecolor
andlinewidth
.By the way I will use a dark theme so that the result looks more like the image you used as a model:
Result:
Variant
Although it's not what you asked for, I think it also has a nice effect, it makes it easier to read the horizontal position of each bar, and it also helps to understand the trick used: