日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Python、Matplot的subplot实现一行3列的子图绘制,并添加背景色

發布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python、Matplot的subplot实现一行3列的子图绘制,并添加背景色 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python、Matplot的subplot實現一行3列的子圖繪制,并添加背景色

    • 1. 可能遇到的問題
    • 2. 示例
      • 1)繪制2*2(倆行倆列)的子圖,并設置背景色
      • 2) 繪制1*3(一行三列)的子圖,并設置橫軸縱軸值
      • 3)繪制1*3(一行三列)的復雜子圖
      • 4)將多個圖4*5(四行五列)顯示在一張圖像上
    • 參考:

1. 可能遇到的問題

AttributeError: ‘AxesSubplot’ object has no property ‘axisbg’

問題描述:使用 Matplotlib subplot繪制2行2列的子圖,并帶有背景色時報如上錯誤
錯誤原因,subplot中沒有 axisbg 屬性
解決辦法:把axisbg 改成 facecolor

2. 示例

1)繪制2*2(倆行倆列)的子圖,并設置背景色

#!/usr/bin/env python
# !encoding=utf-8import matplotlib.pyplot as pltif __name__ == '__main__':for i, color in enumerate("rgby"):# 報錯:AttributeError: 'AxesSubplot' object has no property 'axisbg'# plt.subplot(221 + i, axisbg=color)# 成功plt.subplot(221 + i, facecolor=color)plt.show()

修改后成功如圖:

2) 繪制1*3(一行三列)的子圖,并設置橫軸縱軸值

#!/usr/bin/env python
# !encoding=utf-8import matplotlib.pyplot as plt
import numpy as npif __name__ == '__main__':for i, color in enumerate("rgb"):# 報錯:AttributeError: 'AxesSubplot' object has no property 'axesbg'# plt.subplot(221 + i, axesbg=color)# 成功plt.subplot(131 + i, facecolor=color)# x軸等差數列,y軸等比數列x = i + 1x1 = np.arange(0, x, 1)y1 = np.logspace(1, x, num=x, base=2)print(x1, ' ', y1)plt.plot(x1, y1)plt.show()

3)繪制1*3(一行三列)的復雜子圖

import numpy as np
import matplotlib.pyplot as plt# example data
x = np.arange(0.1, 4, 0.1)
y1 = np.exp(-1.0 * x)
y2 = np.exp(-0.5 * x)# example variable error bar values
y1err = 0.1 + 0.1 * np.sqrt(x)
y2err = 0.1 + 0.1 * np.sqrt(x / 2)# Now switch to a more OO interface to exercise more features.
fig, (ax_l, ax_c, ax_r) = plt.subplots(nrows=1, ncols=3,sharex=True, figsize=(12, 6))ax_l.set_title('all errorbars')
ax_l.errorbar(x, y1, yerr=y1err)
ax_l.errorbar(x, y2, yerr=y2err)ax_c.set_title('only every 6th errorbar')
ax_c.errorbar(x, y1, yerr=y1err, errorevery=6)
ax_c.errorbar(x, y2, yerr=y2err, errorevery=6)ax_r.set_title('second series shifted by 3')
ax_r.errorbar(x, y1, yerr=y1err, errorevery=4)
ax_r.errorbar(x, y2, yerr=y2err, errorevery=3)fig.suptitle('Errorbar subsampling for better appearance')
plt.show()

4)將多個圖4*5(四行五列)顯示在一張圖像上

import osimport matplotlib.image as mpimg
import matplotlib.pyplot as pltfolder_path = os.getcwd().replace('\\', '/')
imgpath = folder_path + "/images/001.jpg"
# matplot讀出來是RGB
image = mpimg.imread(imgpath)for i in range(20):plt.subplot(4, 5, i + 1)plt.imshow(image)  # 通過for循環逐個顯示圖像plt.xticks([])  # 去掉x軸的刻度plt.yticks([])  # 去掉y軸的刻度
plt.show()

參考:

  • https://www.matplotlib.org.cn/tutorials/introductory/sample_plots.html#ellipses

  • https://matplotlib.org/gallery/lines_bars_and_markers/errorbar_subsample.html#sphx-glr-gallery-lines-bars-and-markers-errorbar-subsample-py

  • https://www.cnblogs.com/elitphil/p/12221103.html

總結

以上是生活随笔為你收集整理的Python、Matplot的subplot实现一行3列的子图绘制,并添加背景色的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。