1

I'm sure I've missed something basic but I can't find anything about this by googling so here goes.

I'm trying to plot a bar chart using matplotlib.pyplot but I keep getting an empty bar chart (with the correct axis but no actual bars).

My minimal code to reproduce this is as follows:

import matplotlib.pyplot as plt

plt.bar(
    x=[1288.7, 9386.9, 12086.3, 14785.7, 17485.1, 20184.5, 22883.9, 25596.797],
    height=[0.22772277227722773, 0.5, 0.5, 0.4430379746835443, 0.3658696364231903, 0.39693539122862276, 0.4186823730508119, 0.44525257342712926]
)

plt.show()

I'm actually trying to plot some data calculated in pandas using pd.Crosstabs() e.g.

fig, ax = plt.subplots(1,1)
ax.bar(
    x=list(df.index),
    height=list(df["column1"])
)

But the problem seems to actually be with matplotlib, not with my data.

1 Answers1

3

The problem is that the default bar-width is too small relative to the scale of the x-values. You can set the width as an optional parameter. For example,

import matplotlib.pyplot as plt

plt.bar(
    x=[1288.7, 9386.9, 12086.3, 14785.7, 17485.1, 20184.5, 22883.9, 25596.797],
    height=[0.22772277227722773, 0.5, 0.5, 0.4430379746835443, 0.3658696364231903, 0.39693539122862276, 0.4186823730508119, 0.44525257342712926],
    width = 1e3
)

plt.show()

results in a reasonable graph.

Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16