简体   繁体   English

Python plt.colorbar()的作用是什么,colorbar上的数字表示什么?

[英]Python plt.colorbar() what does it do and what does the numbers on colorbar signifies?

Can someone explain to me what and how colorbar is used? 有人可以向我解释什么以及如何使用颜色条吗? I dont get the significance of colorbar and the number is it shows? 我不知道颜色条的重要性,它显示的数字是多少? What does the number on colorbar means? 彩条上的数字是什么意思? Please explain to me... Data is from datacamp: Two columns with percentage of students taking biology and business from year 1970 to 2010. Below code makes a 2dhist but I dont understand what colorbar labels signifies? 请向我解释...数据来自数据营:两列显示了从1970年到2010年从事生物学和商业工作的学生的百分比。下面的代码可以显示2dhist,但是我不明白颜色条标签的含义是什么? is it percentage?? 是百分比吗?

plt.hist2d(data['Biology'], data['Business'], bins=(5,5))
plt.colorbar()
plt.show()

图片在这里

You have not uploaded your data so I am going to post a toy example and explain some things. 您尚未上传数据,所以我将发布一个玩具示例并解释一些事情。

from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)

# normal distribution center at x=0 and y=5
x = np.random.randn(100000)
y = np.random.randn(100000) + 5

plt.hist2d(x, y, bins=40, norm=LogNorm())
plt.xlabel('x')
plt.ylabel('y')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Counts')
plt.show()

在此处输入图片说明 Here, I have created 2 variables. 在这里,我创建了2个变量。 The x has mean=0 and the y has mean=5 . x mean=0y mean=5 When you plot the 2D hist, you see a 2D histogram. 绘制2D直方图时,您会看到2D直方图。 Think about it like looking at a histogram from the "top". 像从“顶部”看直方图一样思考它。

print(x.mean())
print(y.mean())
#0.0015767005081253399
#5.005093241323296

Now, you can see that the colors at the center of this 2D histogram are yellowish and correspond to the highest values of the colorbar. 现在,您可以看到此2D直方图中心的颜色为淡黄色,并且与色条的最大值相对应。

This is reasonable since the histogram of x should have a peak at 0 and the histogram of y should have a peak at 5 . 这是合理的,因为histogram of x should have a peak at 0histogram of x should have a peak at 0histogram of y should have a peak at 5histogram of y should have a peak at 5

You plot their positions on the xy plane and as you can see they are so dense and overlap with each other. 您可以在xy平面上绘制它们的位置,并且可以看到它们是如此密集并且彼此重叠。 You want to view the distribution better by count of boxes in the plane, so you try a 2D diagram. 您希望通过平面中的框数更好地查看分布,因此您尝试使用2D图。

This is exactly what the colors and the colorbar's values mean. 这正是颜色和颜色条值的含义。 It's the height/frequency/counts of each bin if you had a 1D classical histogram. 如果您具有一维经典直方图,则它是每个仓的高度/频率/计数。


EDIT 1: By using fewer bins it's like zooming in the initial plot. 编辑1:通过使用较少的垃圾箱,就像放大初始图。

from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)

# normal distribution center at x=0 and y=5
x = np.random.randn(100000)
y = np.random.randn(100000) + 5

plt.hist2d(x, y, bins=(5,5), norm=LogNorm())
plt.xlabel('x')
plt.ylabel('y')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Counts')
plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM