Reputation: 9760
All the matplotlib examples with hist()
generate a data set, provide the data set to the hist
function with some bins (possibly non-uniformly spaced) and the function automatically calculates and then plots the histogram.
I already have histogram data and I simply want to plot it, how can I do that?! For example, I have the bins (half open ranges are denoted by the square and curved bracket notation),
[0, 1) 0
[1, 2) 3
[2, 3) 8
[3, 4) 6
[4, 5) 2
[5, 6) 3
[6, 7) 1
[7, 8) 0
Upvotes: 22
Views: 22963
Reputation: 91
I'm surprised nobody mentioned plt.step here yet for making step plots...
a= [1,2,3,4,5,6,7,8,9]
b= [5,3,4,5,3,2,1,2,3]
plt.step(a,b)
Upvotes: 3
Reputation: 5647
Also, as an alternative (similar to Matlab), you can use bar
:
import matplotlib.pyplot as plt
a= [1,2,3,4,5,6,7,8,9]
b= [5,3,4,5,3,2,1,2,3]
plt.bar(a,b)
Then, you can also add the title and other stuff and, finally, save the image:
plt.title("Clock cycles")
plt.grid()
plt.xlabel("Size of the matrices processed")
plt.ylabel("Clock cycles")
plt.savefig("clock_cycles.svg")
Upvotes: 3
Reputation: 1140
Perhaps the weight parameter would be of help in your problem.
import matplotlib.pyplot as plt
a= [1,2,3,4,5,6,7,8,9]
b= [5,3,4,5,3,2,1,2,3]
plt.hist(a,9, weights=b)
plt.show()
Or, as tcaswell said, you could just make a bar plot and change the x-axis.
Using matplotlib how could I plot a histogram with given data in python
Is a link.
Upvotes: 24