How to plot scatter using histogram output in matplotlib?

I want to build a scatter plot like this:

enter image description here

I can plot a histogram from my data, but I need a scatter plot for the same data. Is there a way I can use the output of the hist () method as input to scatter the graph? or any other way is to plot a scatter using the hist () method in matplotlib? The code is used to build a histogram as follows:

data = get_data() plt.figure(figsize=(7,4)) ax = plt.subplots() plt.hist(data,histtype='bar',bins = 100,log=True) plt.show() 
+4
source share
1 answer

I think you are looking for the following:

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

 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 come up with without any more detailed description of the problem. Sorry if I misunderstood.

+2
source

Source: https://habr.com/ru/post/1497899/


All Articles