如何在matplotlib/seaborn子地块上使用相同的调色板?
我正在用matplotlib和seaborn制作一组子图,我希望顶部和底部子图的调色板相同 奇怪的是,对于某些类型的打印(例如顶部的lineplot和底部的distplot),调色板自动相同。其他情况下(如顶部的regplot和底部的distplot),则不是 这是我的意思的一个例子:如何在matplotlib/seaborn子地块上使用相同的调色板?,matplotlib,seaborn,Matplotlib,Seaborn,我正在用matplotlib和seaborn制作一组子图,我希望顶部和底部子图的调色板相同 奇怪的是,对于某些类型的打印(例如顶部的lineplot和底部的distplot),调色板自动相同。其他情况下(如顶部的regplot和底部的distplot),则不是 这是我的意思的一个例子: import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib import seaborn as
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
import itertools
sns.set(style='darkgrid')
x = np.arange(1,1001)
x=np.random.lognormal(0,1,1001)
fig1, ax1 = plt.subplots(2,1)
sns.lineplot(x, x*2, label ='2x', ax=ax1[0])
sns.lineplot(x, x*3, label ='3x', ax=ax1[0])
sns.distplot( 2*x, bins = 100, ax = ax1[1], label='2x' )
sns.distplot( 3*x, bins = 100, ax = ax1[1], label='3x' )
for a in ax1:
a.legend()
fig2, ax2 = plt.subplots(2,1)
sns.regplot(x, x*2, label ='2x', ax=ax2[0])
sns.regplot(x, x*3, label ='3x', ax=ax2[0])
sns.distplot( 2*x, bins = 100, ax = ax2[1], label='2x' )
sns.distplot( 3*x, bins = 100, ax = ax2[1], label='3x' )
for a in ax2:
a.legend()
因此,下面的代码确实有效
然而,我想知道:
color=next(调色板)
这确实出乎意料。请注意,调色板始终是相同的。这叫做房地产周期。有趣的是,lineplot+distplot在每个轴上显示该循环的前两种颜色,正如人们所期望的那样。但regplot+distplot在第一个轴上显示前两种颜色,在其他轴上显示颜色3和4。这很令人惊讶。值得一份bug报告吗?
palette = itertools.cycle(sns.color_palette())
fig3, ax3 = plt.subplots(2,1)
sns.regplot(x, x*2, label ='2x', ax=ax3[0], color = next(palette) )
sns.regplot(x, x*3, label ='3x', ax=ax3[0], color = next(palette))
palette = itertools.cycle(sns.color_palette())
sns.distplot( 2*x, bins = 100, ax = ax3[1], label='2x', color = next(palette) )
sns.distplot( 3*x, bins = 100, ax = ax3[1], label='3x' , color = next(palette) )
for a in ax3:
a.legend()