我更喜欢使用matplotlibOOP风格:
f, axarr = plt.subplots(2, sharex=True)
axarr[0].plot(...)
axarr[1].plot(...)
Run Code Online (Sandbox Code Playgroud)
这样可以更轻松地跟踪多个图形和子图.
问题:如何使用seaborn这种方式?或者,如何将此示例更改为OOP样式?如何将seaborn绘图函数描述为lmplot哪个Figure或哪个Axes绘图?
查看matplotlib文档,似乎添加AxesSubplot到a 的标准方法Figure是使用Figure.add_subplot:
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.hist( some params .... )
Run Code Online (Sandbox Code Playgroud)
我希望能够AxesSubPlot独立于图形创建类似对象,因此我可以在不同的图中使用它们.就像是
fig = pyplot.figure()
histoA = some_axes_subplot_maker.hist( some params ..... )
histoA = some_axes_subplot_maker.hist( some other params ..... )
# make one figure with both plots
fig.add_subaxes(histo1, 211)
fig.add_subaxes(histo1, 212)
fig2 = pyplot.figure()
# make a figure with the first plot only
fig2.add_subaxes(histo1, 111)
Run Code Online (Sandbox Code Playgroud)
这是可能的matplotlib,如果可以,我该怎么做?
更新:我还没有设法解除Axes和Figures的创建,但是下面的答案中的示例可以很容易地在new或olf Figure实例中重用以前创建的轴.这可以通过一个简单的功能来说明:
def plot_axes(ax, fig=None, …Run Code Online (Sandbox Code Playgroud) 我试图将以下两个图放在同一个图上:
import seaborn as sns; sns.set(color_codes=True)
import matplotlib.pyplot as plt
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
iris = sns.load_dataset("iris")
sns.boxplot(data=iris, orient="h", palette="Set2", ax = ax1)
species = iris.pop("species")
lut = dict(zip(species.unique(), "rbg"))
row_colors = species.map(lut)
sns.clustermap(iris, row_colors=row_colors, ax = ax2)
Run Code Online (Sandbox Code Playgroud)
我知道 clustermap 返回一个数字,所以这不起作用。但是,我仍然需要一种方法来将这些图彼此相邻(水平)呈现。sns.heatmap 返回一个轴,但它不支持聚类或颜色注释。
做这个的最好方式是什么 ?
将 seaborn 图形添加到子图中通常是通过在创建图形时传递 'ax'来完成的。例如:
sns.kdeplot(x, y, cmap=cmap, shade=True, cut=5, ax=ax)
Run Code Online (Sandbox Code Playgroud)
但是,此方法不适用于seaborn.palplot,它可视化 seaborn 调色板。我的目标是创建一个不同调色板的图形,用于可扩展的颜色比较和演示。此图像粗略地显示了我正在尝试创建的图 [源]。
一个可能相关的答案描述了一种创建 seaborn 图形并将轴复制到另一个图形的方法。我一直无法将这种方法应用于 palplot 图形,并且想知道是否有一种快速的方法可以将它们强制转换为现有图形。
这是我的最小工作示例,现在仍在生成单独的数字。
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
fig1 = plt.figure()
length, n_colors = 12, 50 # amount of subplots and colors per subplot
start_colors = np.linspace(0, 3, length)
for i, start_color in enumerate(start_colors):
ax = fig1.add_subplot(length, 1, i + 1)
colors = sns.cubehelix_palette(n_colors=n_colors, …Run Code Online (Sandbox Code Playgroud) 到目前为止我已经尝试过以下代码:
# Import to handle plotting
import seaborn as sns
# Import pyplot, figures inline, set style, plot pairplot
import matplotlib.pyplot as plt
# Make the figure space
fig = plt.figure(figsize=(2,4))
gs = fig.add_gridspec(2, 4)
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, :])
# Load the example car crash dataset
tips = sns.load_dataset("tips")
# Plot the frequency counts grouped by time
sns.catplot(x='sex', hue='smoker',
kind='count',
col='time',
data=tips,
ax=ax1)
# View the data
sns.catplot(x='sex', y='total_bill', hue='smoker',
kind='violin',
col='time',
split='True',
cut=0,
bw=0.25,
scale='area', …Run Code Online (Sandbox Code Playgroud)