【plt.hist】—Solution to the problem of histogram loss when drawing multiple histograms

Project scenario:

At present, there are two types of data, and it is necessary to draw histograms separately and place them in the same Figure for comparison to determine the difference in characteristic data between the two types.

The solution is documented in this article.


Problem Description

Multiple pieces of data are used, and these data can be divided into two categories. The category set is stored in group_label, and the category of each piece of data is stored in label. By looping through all group_labels, different categories of data histograms are drawn.

fig = plt.figure()
for each in group_label:	
	# label为
    fig_data = data[[True if i == each else False for i in label]]
    plt.hist(fig_data[:, dim], alpha = 0.8, label=each, density=False)
plt.legend()

When using a for loop to draw a histogram, the histogram of a certain category is lost.
insert image description here
As shown in the figure above, the 0 category is not displayed in the histogram.


Cause Analysis:

First, start by plotting histograms for the two categories separately to see the difference.
insert image description here

insert image description here
It is found that the abscissa ranges of the two are different, and the difference is very large:

类别0: (0, 6)
类别1: (1.81571, 1.815715)

In the histogram drawn at the same time, the range of abscissa is:

(0.75, 2.75)

It is found here that there may be a problem with the coordinate range.


solution:

There is a range parameter in plt.hist to limit the interval range of the drawing

Change the code as follows

fig = plt.figure()
data_min = np.min(data, axis=0)
data_max = np.max(data, axis=0)
for each in group_label:
	fig_data = data[[True if i == each else False for i in label]]
	plt.hist(fig_data[:, dim], alpha = 0.8, label=each, range=(data_min[dim], data_max[dim]))
plt.legend()

That is, use range to limit the range of the abscissa to solve the problem of partial histogram loss.

The renderings are as follows:
insert image description here

Guess you like

Origin blog.csdn.net/qq_48691686/article/details/124502676