问题
I would like to change the colors for each histogram in a jointplot, created with seaborn.
I managed to change the color for both plots using marginal_kws, but how can I set a color for one histogram each? (e. g. red and green histogram)
A minimal example of my jointplot:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
x, y = np.random.multivariate_normal([2, 3], [[0.3, 0], [0, 0.5]], 1000).T
with sns.axes_style("white"):
g = sns.jointplot(x=x, y=y, kind="hex", stat_func=None, marginal_kws={'color': 'green'})
plt.show()
回答1:
iayork's answer about using the axes objects directly is good, although another option would be to change the color of the bars after plotting:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="white", color_codes=True)
x, y = np.random.multivariate_normal([2, 3], [[0.3, 0], [0, 0.5]], 1000).T
g = sns.jointplot(x=x, y=y, kind="hex", stat_func=None, marginal_kws={'color': 'green'})
plt.setp(g.ax_marg_y.patches, color="r")
回答2:
I think you need to use jointgrid rather than jointplot here. Here's an attempt to get something close to your present plot; you will probably need to play with colors and cmaps more to make the hexbin plot look more attractive.
x, y = np.random.multivariate_normal([2, 3], [[0.3, 0], [0, 0.5]], 1000).T
def hexbin(x, y):
plt.hexbin(x, y, gridsize=20, cmap='Blues')
with sb.axes_style("white"):
g = sb.JointGrid(x=x, y=y, ylim=(0,6))
g = g.plot_joint(hexbin)
g.ax_marg_x.hist(x, color="b", alpha=.6)
g.ax_marg_y.hist(y, color="r", alpha=.6, orientation="horizontal")
来源:https://stackoverflow.com/questions/32046029/pythons-seaborn-jointplot-different-colors-for-each-histograms