Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a scatter plot using the histogram output in matplotlib?

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()
like image 770
user1944257 Avatar asked Aug 20 '13 01:08

user1944257


People also ask

Is a scatter plot a histogram?

Those charts are: Histograms: To see the distribution of a set of continuous data. Pareto charts: A bar chart, each bar length represent the frequency, and it is arranged from longest to shortest. Scatter plots: Plots observations as points to show the relationship between two sets of data.


1 Answers

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.

like image 56
Greg Avatar answered Oct 05 '22 18:10

Greg