Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a bar plot with matplotlib using two single values?

I am trying to make a bar plot with two values for two classes A and B using matplotlib. My values are a = 43 and b = 21.

I need the plot to look like this:

enter image description here

I have been trying to do it almost an hour using matplotlib examples but eventually gave up. Maybe somebody can help me out?

like image 279
minerals Avatar asked Feb 28 '26 21:02

minerals


2 Answers

As of mpl 1.5 you can simply do:

import matplotlib.pyplot as plt

fig, ax  = plt.subplots()
ax.bar([1, 2], [43, 21], width=1,
       tick_label=['A', 'B'], align='center')

enter image description here

like image 98
tacaswell Avatar answered Mar 02 '26 09:03

tacaswell


Use bar():

import matplotlib.pyplot as plt

fig = plt.figure()
s = fig.add_subplot(111)
s.bar([1, 2], [43, 21], width=1)
s.set_xlim(0.5, 3.5)
fig.savefig('t.png')

enter image description here

Edit: following the specs more accurately.

like image 29
fjarri Avatar answered Mar 02 '26 09:03

fjarri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!