7

I want to plot a scatter plot similar to this one :

enter image description here

I can plot a histogram from my data but i want a scatter plot for the same data . Is there any way i can use the hist() method output as an input to scatter plot? or some other way is there to plot scatter plot using the hist() method in matplotlib? The code is use to plot histogram is as follows :

data = get_data()
plt.figure(figsize=(7,4))
ax = plt.subplots()
plt.hist(data,histtype='bar',bins = 100,log=True)
plt.show()
tacaswell
  • 84,579
  • 22
  • 210
  • 199
user1944257
  • 83
  • 1
  • 1
  • 5

1 Answers1

5

I think you're looking for the following:

Essentially plt.hist() outputs two arrays (and as Nordev pointed out some patches). The first is the count in each bin (n) and the second the edges of the bin.

import matplotlib.pylab as plt
import numpy as np

# Create some example data
y = np.random.normal(5, size=1000)

# Usual histogram plot
fig = plt.figure()
ax1 = fig.add_subplot(121)
n, bins, patches = ax1.hist(y, bins=50)  # output is two arrays

# Scatter plot
# Now we find the center of each bin from the bin edges
bins_mean = [0.5 * (bins[i] + bins[i+1]) for i in range(len(n))]
ax2 = fig.add_subplot(122)
ax2.scatter(bins_mean, n)

Example

This is the best I can think of without some more description of the problem. Sorry if I misunderstood.

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
Greg
  • 11,654
  • 3
  • 44
  • 50
  • The `output` consists not only of two arrays, but also a list of `Patch` objects. Why not use the "standard" `n, bins, patches = ax1.hist(...` as this unpacks the returned arrays/list to the respective variables? IMO these are more intuitive variable names and makes it easier to read the code. – sodd Aug 20 '13 at 08:58
  • If you don't want the patches, just use `np.histogram`. `plt.hist` is just a wrapper around `histogram` that plots the result using `plt.bar`. – tacaswell Aug 20 '13 at 13:09