亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Jupyter 單元格中并排顯示兩個圖形

如何在 Jupyter 單元格中并排顯示兩個圖形

白板的微信 2023-10-06 16:23:55
import pandas as pdimport seaborn as sns# load datadf = sns.load_dataset('penguins', cache=False)sns.scatterplot(data=df, x='bill_length_mm', y='bill_depth_mm', hue='sex')plt.show()sns.scatterplot(data=df, x='flipper_length_mm', y='body_mass_g', hue='sex')plt.show()當我在 jupyter 的一個單元格中使用 seaborn 繪制兩個圖時,我得到以下視圖:我想并排繪制這些圖,如下所示:繪圖 1 繪圖 2我應該怎么做?更新:不是一張圖上的兩個圖,而是兩個單獨圖上的兩個圖。這不是正在尋求的解決方案,因為它是一張圖上的兩個圖。fig, ax = plt.subplots(1,2) sns.plotType(someData, ax=ax[0])  # plot1 sns.plotType(someData, ax=ax[1])  # plot2 fig.show()建議的重復ipython 筆記本中的解決方案水平排列繪圖,不起作用選項 導致%html圖形彼此重疊繪制此外,其他選項適用于ipython,而不是 Jupyter,或建議創建子圖。
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

這可能是最簡單的解決方案。其他解決方案可能涉及入侵 Jupyter 的后端環境。

本題涉及并排顯示兩個圖形。

從代碼單元中并排執行的兩個單獨的圖形不起作用。

您將需要創建單獨的圖形,并用于plt.savefig('file.jpg')將每個圖形保存到文件中。

import pandas as pd

import seaborn as sns

import matplotlib.pyplot as plt


# load data

df = sns.load_dataset('penguins', cache=False)


# create and save figure

sns.scatterplot(data=df, x='bill_length_mm', y='bill_depth_mm', hue='sex')

plt.savefig('bill.jpg')

plt.close()  # prevents figure from being displayed when code cell is executed


# create and save new figure

sns.scatterplot(data=df, x='flipper_length_mm', y='body_mass_g', hue='sex')

plt.savefig('flipper.jpg')

plt.close()  # prevents figure from being displayed when code cell is executed

將圖形保存到文件后,可以通過將它們加載到 Markdown 單元格中并排顯示它們。

如果圖像太大,第二個數字將換行。

https://img1.sycdn.imooc.com//651fc46a0001a12a04250099.jpg

  • 然后執行單元格


https://img1.sycdn.imooc.com//651fc47b0001ce1006500195.jpg

查看完整回答
反對 回復 2023-10-06
  • 1 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號