如何将python的ggplot对象添加到matplot网格?

2024-04-26 21:10:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我的pandasDataFrameresults_print有2维数组,它们是图像。我是这样打印的:

n_rows = results_print.shape[0]
n_cols = results_print.shape[1]
f, a = plt.subplots(n_cols, n_rows, figsize=(n_rows, n_cols))
methods = ['img', 'sm', 'rbd', 'ft', 'mbd', 'binary_sal', 'sal']
for r in range(n_rows):
    for c, cn in zip(range(len(methods)), methods):
        a[c][r].imshow(results_print.at[r,cn], cmap='gray')

现在我创建了一个python ggplot image对象:

^{pr2}$

如何将gg作为元素添加到matplotlib网格中?在


Tags: in图像forrangeplt数组cnresults
1条回答
网友
1楼 · 发布于 2024-04-26 21:10:59

我想解决办法是先画出ggplot部分。然后通过plt.gcf()获得matplotlib图形对象,通过plt.gca()获得轴。调整ggplot轴的大小以适合网格,并最终将matplotlib图的其余部分绘制到该图形中。在

enter image description here

import ggplot as gp
import matplotlib.pyplot as plt
import numpy as np
# make ggplot
g = gp.ggplot(gp.aes(x='carat', y='price'), data=gp.diamonds)
g = g + gp.geom_point()
g = g + gp.ylab(' ')+ gp.xlab(' ')
g.make()
# obtain figure from ggplot
fig = plt.gcf()
ax = plt.gca()
# adjust some of the ggplot axes' parameters
ax.set_title("ggplot plot")
ax.set_xlabel("Some x label")
ax.set_position([0.1, 0.55, 0.4, 0.4])

#plot the rest of the maplotlib plots
for i in [2,3,4]:
    ax2 = fig.add_subplot(2,2,i)
    ax2.imshow(np.random.rand(23,23))
    ax2.set_title("matplotlib plot")
plt.show()

相关问题 更多 >