Tristan Klassen
Tristan Klassen

Reputation: 435

Unfilled bar plot in matplotlib

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

Upvotes: 21

Views: 36761

Answers (3)

Benjamin schwarz
Benjamin schwarz

Reputation: 109

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)))

Upvotes: 1

isosceleswheel
isosceleswheel

Reputation: 1546

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

Upvotes: 10

XValidated
XValidated

Reputation: 1011

[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

Upvotes: 18

Related Questions