使用 MatplotLib 直方图可视化第三个变量
Visualizing third variable with MatplotLib Histograms
请原谅我的英语不好。
在如下所示的 DataFrame 上:
-----------------
|index|var1|var2|
-----------------
有很多行
var1 介于 0 和 4000 之间
var2 介于 -100 和 100
之间
我想创建一个直方图,根据 var1 显示有多少行。
在 Y 轴上,我们可以看到有多少行,例如 0 > var1 < 500,大约有 500k 行。
现在我想添加var2,它显示一行的质量。
例如,我希望直方图根据 var2 的值从 0 到 500 变成蓝色,另一种颜色从 500 到 1000(比如如果条形图作为 var2 的平均值为 100 的值,如果平均值为 0,则将其设为绿色, 让它变红).
我试着硬核这个,但是一旦我改变了 bin 或任何东西,我的代码就崩溃了。
我也试过用 plot on the top of the hist 来做,但是没用。
我当前的截图代码:
plt.hist(var1, bins=10, range=(0,4000), color='orange', alpha=0.7)
plt.title('Var 1',weight='bold', fontsize=18)
plt.yticks(weight='bold')
plt.xticks(weight='bold')
我觉得这是很简单的事情,但我因此完全陷入了学习。
非常感谢您的帮助。
如果您创建一个包含直方图中每个条形颜色的列表,您可以使用以下代码片段。它捕获 plt.hist
命令的 return 值,其中包括各个补丁。可以在遍历这些补丁时单独设置颜色。
n, bins, patches = plt.hist(var1, bins=8, range=(0,4000), color="orange", alpha=0.7)
for i, patch in enumerate(patches):
plt.setp(patch, "facecolor", colors[i])
此外,这是一种根据您拥有的数据类型创建上述 color list
的可能方法:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# create random values and store them in a DataFrame
y1 = np.random.randint(0,4000, 50)
y2 = np.random.randint(-100, 101, 50)
y = zip(y1,y2)
df = pd.DataFrame(y, columns=["Var1","Var2"])
var1 = df["Var1"].values
# pd.cut to bin the dataframe in the appropriate ranges of Var1
# then the mean of Var2 is calculated for each bin, results are stored in a list
mean = [df.groupby(pd.cut(df["Var1"], np.arange(0, 4000+500, 500)))["Var2"].mean()]
# how to color the bars based on Var2:
# -100 <= mean(Var2) < -33: blue
# -33 <= mean(Var2) < 33: red
# 33 <= mean(Var2) < 100: green
color_bins = np.array([-100,-33,33,100])
color_list = ["blue","red","green"]
# bin the means of Var2 according to the color_bins we just created
inds = np.digitize(mean, color_bins)
# list that assigns the appropriate color to each patch
colors = [color_list[value-1] for value in inds[0]]
n, bins, patches = plt.hist(var1, bins=8, range=(0,4000), color="orange", alpha=0.7)
for i, patch in enumerate(patches):
plt.setp(patch, "facecolor", colors[i])
plt.title('Var 1',weight='bold', fontsize=18)
plt.yticks(weight='bold')
plt.xticks(weight='bold')
plt.show()
请原谅我的英语不好。
在如下所示的 DataFrame 上:
-----------------
|index|var1|var2|
-----------------
有很多行 var1 介于 0 和 4000 之间 var2 介于 -100 和 100
之间我想创建一个直方图,根据 var1 显示有多少行。
在 Y 轴上,我们可以看到有多少行,例如 0 > var1 < 500,大约有 500k 行。
现在我想添加var2,它显示一行的质量。 例如,我希望直方图根据 var2 的值从 0 到 500 变成蓝色,另一种颜色从 500 到 1000(比如如果条形图作为 var2 的平均值为 100 的值,如果平均值为 0,则将其设为绿色, 让它变红).
我试着硬核这个,但是一旦我改变了 bin 或任何东西,我的代码就崩溃了。
我也试过用 plot on the top of the hist 来做,但是没用。
我当前的截图代码:
plt.hist(var1, bins=10, range=(0,4000), color='orange', alpha=0.7)
plt.title('Var 1',weight='bold', fontsize=18)
plt.yticks(weight='bold')
plt.xticks(weight='bold')
我觉得这是很简单的事情,但我因此完全陷入了学习。
非常感谢您的帮助。
如果您创建一个包含直方图中每个条形颜色的列表,您可以使用以下代码片段。它捕获 plt.hist
命令的 return 值,其中包括各个补丁。可以在遍历这些补丁时单独设置颜色。
n, bins, patches = plt.hist(var1, bins=8, range=(0,4000), color="orange", alpha=0.7)
for i, patch in enumerate(patches):
plt.setp(patch, "facecolor", colors[i])
此外,这是一种根据您拥有的数据类型创建上述 color list
的可能方法:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# create random values and store them in a DataFrame
y1 = np.random.randint(0,4000, 50)
y2 = np.random.randint(-100, 101, 50)
y = zip(y1,y2)
df = pd.DataFrame(y, columns=["Var1","Var2"])
var1 = df["Var1"].values
# pd.cut to bin the dataframe in the appropriate ranges of Var1
# then the mean of Var2 is calculated for each bin, results are stored in a list
mean = [df.groupby(pd.cut(df["Var1"], np.arange(0, 4000+500, 500)))["Var2"].mean()]
# how to color the bars based on Var2:
# -100 <= mean(Var2) < -33: blue
# -33 <= mean(Var2) < 33: red
# 33 <= mean(Var2) < 100: green
color_bins = np.array([-100,-33,33,100])
color_list = ["blue","red","green"]
# bin the means of Var2 according to the color_bins we just created
inds = np.digitize(mean, color_bins)
# list that assigns the appropriate color to each patch
colors = [color_list[value-1] for value in inds[0]]
n, bins, patches = plt.hist(var1, bins=8, range=(0,4000), color="orange", alpha=0.7)
for i, patch in enumerate(patches):
plt.setp(patch, "facecolor", colors[i])
plt.title('Var 1',weight='bold', fontsize=18)
plt.yticks(weight='bold')
plt.xticks(weight='bold')
plt.show()