私が取り組んでいるものの最小限のコード。いくつかのパラメータは重複しているように見えるかもしれませんが、すべてを削除することはありませんでした。コメントアウトmultipart seabornプロットでmatplotlib gridspecを繰り返し挿入するにはどうすればいいですか?
すべてseabornプロット(g)でコードを実行するimport matplotlib
import matplotlib.gridspec as gridspec
matplotlib.use("macosx")
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
def plot_overlaid_2d_hist(data,
plot_axis_x,
plot_axis_y,
plot_axis_x_lab,
plot_axis_y_lab,
group_by = "group_name"):
# don't mind this for now
df = data
# Figure aspect
w, h = plt.figaspect(1)
fig = plt.figure(figsize = (w, h))
# Count the number of groups to make plots for
n_groups = len(df.groupby(group_by))
gs = gridspec.GridSpec(nrows = n_groups, ncols = 1)
subplot_id = 0
# Reshape data to make it work
for name, group in df.groupby(group_by, sort = False):
# Initialize subplot
fig.add_subplot(gs[subplot_id, 0])
# Check if we get subplots with pyplot
if subplot_id == 0:
col = "red"
else:
col = "blue"
plt.plot(x, y, color = col)
# instantiate JointGrid
# g = sns.JointGrid(group[plot_axis_x],
# group[plot_axis_y],
# data = group,
# space = 0,
# xlim = (0, 1.2),
# ylim = (0, 1))
#
# # Fix labels
# g = g.set_axis_labels(xlabel = str(plot_axis_x_lab),
# ylabel = str(plot_axis_y_lab))
#
# # center scatter plot on top
# g = g.plot_joint(plt.scatter,
# s = 0.5,
# alpha = 1,
# linewidth = 1)
#
# # marginals plot
# g = g.plot_marginals(sns.distplot,
# kde = True,
# kde_kws = dict(linewidth = 2,
# alpha = 1,
# bw = "Scott"),
# hist_kws = dict(alpha = 1))
# Next plot in row +1
subplot_id += 1
# Output
plt.tight_layout() # Attempts to fix alignment of subplot layout and axis titles
plt.show()
# quick data to check if the plots end up where they should
x = [0.5, 0.5, 0.4, 0.4]
y = [0.6, 0.4, 0.3, 0.4]
grp = ["a", "a", "b", "b"]
df = pd.DataFrame({"x":x,
"y":y,
"grp": grp})
plot_overlaid_2d_hist(data = df,
group_by = "grp",
plot_axis_x_lab = "x",
plot_axis_y_lab = "x",
plot_axis_y = "x",
plot_axis_x = "x")
は、それがネイティブpyplotのため正常に動作することを示しているが、私はマルチパートseabornプロットに追加するときに、彼らは別の人物に表示されます。私が望むのは、各2Dヒストグラムと余白と散布図に独自のグリッドスペックの行/列を設定することです。
ドキュメントをお読みになりましたか? 'JointGrid'はそれ自身のFigureを作成します。もしそれが起こらないようにするには、 'JointGrid'を使わないでください。 – ImportanceOfBeingErnest
私はそこに道があると思った。実際には、https://github.com/mwaskom/seaborn/issues/399があることが分かりますが、まだ正式ではありません。 –
その解決策は海底のソースコードの変更に依存していることを確かめてください。それをやりたいのであれば、途中で遭遇する問題についてはもちろん質問することができます。 – ImportanceOfBeingErnest