17

With histograms, there's a simple built-in option histtype='step'. How do I make a bar plot in the same style?

Tristan Klassen
  • 395
  • 1
  • 3
  • 9
  • is plt.bar(x, y, color="None") what you want? – gcalmettes Aug 09 '12 at 21:49
  • No, because I want to eliminate the vertical lines where bars adjoin each other. – Tristan Klassen Aug 09 '12 at 21:52
  • 1
    http://stackoverflow.com/questions/11297030/matplotlib-stepped-histogram-with-already-binned-data I found the answer here. This is actually exactly what I wanted to do (use bar plot as histogram), but I didn't find it because I was searching for questions about bar plots. – Tristan Klassen Aug 10 '12 at 16:22

3 Answers3

16

[adding answer after reading the comments] Set the optional keyword to be fill=False for bar plots:

import matplotlib.pyplot as plt

plt.bar(bins[:5], counts[:5], fill=False, width=60)  # <- this is the line

plt.title("Number of nodes with output at timestep")
plt.xlabel("Node count")
plt.ylabel("Timestep (s)")

will give: enter image description here

Or use plt.plot with the keyword ls='steps' :

plt.plot(bins[-100:], counts[-100:], ls='steps')
plt.title("Number of nodes with output at timestep")
plt.xlabel("Node count")
plt.ylabel("Timestep (s)")

enter image description here

XValidated
  • 981
  • 8
  • 12
  • the second graph was just what I thought he was asking for, and it was what I was looking for too. – jimh Sep 14 '16 at 07:00
  • Thanks for your answer! But can we also avoid the inner edge of the barplot? say that, the vertical line at x=250? – Xu Shan Jun 22 '22 at 08:28
10

Although OP linked to a post that answered a slightly different question relating to histogram step plots, here is a solution for anyone passing through here who is specifically trying to turn off the face color in pyplot.bar bar plots:

import matplotlib.pyplot as plt
import numpy as np

# create x coords for the bar plot
x = np.linspace(1, 10, 10)

# cook up some random bar heights -- exact results may vary :-P
y = np.random.randn(10)
z = np.random.randn(10) * 2

# plot bars with face color off
plt.bar(x-0.2, y, width=0.4, edgecolor='purple', color='None')
plt.bar(x+0.2, z, width=0.4, edgecolor='darkorange', color='None')
plt.show()

enter image description here

Note that bar edges have settable matplotlib.lines.Line2D attributes, such as linewidth, linestyle, alpha, et cetera:

plt.bar(x-0.2, y, width=0.4, edgecolor='purple', color='None', 
linewidth=0.75, linestyle='--')
plt.bar(x+0.2, z, width=0.4, edgecolor='darkorange', color='None',
linewidth=1.5, linestyle='-.')
plt.show()

enter image description here

isosceleswheel
  • 1,516
  • 12
  • 20
1

I saw you found an answer on this other topic, nonetheless I have the feeling matplotlib.pyplot.step does the job too and is more direct (see here).

Edit: as requested, some sample code to illustrate usage of plt.step

import matplotlib.pyplot as plt
plt.step(list(range(10)),list(range(5))+list(range(5)))
  • Please provide code using `matplotlib.pyplot.step` answering the question, as an illustration. – PiCTo Dec 03 '19 at 18:55