我的kdeplot的颜色栏有问题。它应显示每个垃圾箱中的百分比(从0%开始)。我尝试了两种不同的方法,但是两种可视化都不完全是我所需要的。
具有JointGrid的版本确实以0%开头,但不显示每种颜色的其他值。此外,我需要“背景”为白色或至少明亮(不是黑色,而是阴影)。
带关节图的版本显示每种颜色的值,但不显示百分比。
这是我用来创建可视化的代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
x = np.random.normal(np.tile(np.random.uniform(-5, 35, 10), 1000), 4)
y = np.random.normal(np.tile(np.random.uniform(910, 1030, 10), 1000), 4)
data = pd.DataFrame(x,y)
g = sns.JointGrid(x, y, data=data, space=0, xlim=[10,40], ylim=[920,1020])
g = g.plot_joint(sns.kdeplot, cmap="Blues_d", shade=True, cbar=True, cbar_kws=
{'format':'%.0f%%','ticks': [0, 100]})
g = g.plot_marginals(sns.kdeplot, shade=True)
plt.subplots_adjust(left=0.1, right=0.8, top=0.9, bottom=0.1)
pos_joint_ax = g.ax_joint.get_position()
pos_marg_x_ax = g.ax_marg_x.get_position()
g.ax_joint.set_position([pos_joint_ax.x0, pos_joint_ax.y0, pos_marg_x_ax.width, pos_joint_ax.height])
g.fig.axes[-1].set_position([.83, pos_joint_ax.y0, .07, pos_joint_ax.height])
plt.show()
kdeplot = sns.jointplot(x, y, kind="kde", cbar=True, xlim=[10,40], ylim=[920,1020])
plt.subplots_adjust(left=0.1, right=0.8, top=0.9, bottom=0.1)
pos_joint_ax = kdeplot.ax_joint.get_position()
pos_marg_x_ax = kdeplot.ax_marg_x.get_position()
kdeplot.ax_joint.set_position([pos_joint_ax.x0, pos_joint_ax.y0, pos_marg_x_ax.width,
pos_joint_ax.height])
kdeplot.fig.axes[-1].set_position([.83, pos_joint_ax.y0, .07, pos_joint_ax.height])
plt.show()
有人可以在这里帮助我吗?我真的迷路了。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
x = np.random.normal(np.tile(np.random.uniform(15, 35, 10), 1000), 4)
y = np.random.normal(np.tile(np.random.uniform(940, 1000, 10), 1000), 10)
kdeplot = sns.jointplot(x, y, kind="kde", cbar=True, xlim=[10, 40], ylim=[920, 1020])
plt.subplots_adjust(left=0.1, right=0.8, top=0.9, bottom=0.1)
pos_joint_ax = kdeplot.ax_joint.get_position()
pos_marg_x_ax = kdeplot.ax_marg_x.get_position()
kdeplot.ax_joint.set_position([pos_joint_ax.x0, pos_joint_ax.y0, pos_marg_x_ax.width,
pos_joint_ax.height])
kdeplot.fig.axes[-1].set_position([.83, pos_joint_ax.y0, .07, pos_joint_ax.height])
# get the current colorbar ticks
cbar_ticks = kdeplot.fig.axes[-1].get_yticks()
# get the maximum value of the colorbar
_, cbar_max = kdeplot.fig.axes[-1].get_ylim()
# change the labels (not the ticks themselves) to a percentage
kdeplot.fig.axes[-1].set_yticklabels([f'{t / cbar_max * 100:.1f} %' for t in cbar_ticks])
plt.show()