python - 用 Matplotlib 不代表半个像素
问题描述
有人知道是否可以不使用 表示对角矩阵的一半像素plt.imshow()
吗?
这以图形方式表达了我正在寻找的内容:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import matplotlib
bins = 5
Z = np.random.rand(bins, bins)
# Select lower triangle values:
condition = np.tril(np.ones((Z.shape))).astype(np.bool)
Z = np.where(condition, Z, np.nan)
fig, ax = plt.subplots(figsize = (8,8))
ax.imshow(Z, cmap = 'Spectral')
我想这可以通过用蒙版覆盖图像来完成,但这是我宁愿避免的选项。
解决方案
您可以Patch
在 matplotlib 中将对象用作剪贴蒙版。见https://matplotlib.org/3.1.0/gallery/images_contours_and_fields/image_clip_path.html
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import matplotlib
bins = 5
Z = np.random.rand(bins, bins)
# Select lower triangle values:
condition = np.tril(np.ones((Z.shape))).astype(np.bool)
Z = np.where(condition, Z, np.nan)
fig, ax = plt.subplots()
im = ax.imshow(Z, cmap = 'Spectral')
tri = matplotlib.patches.Polygon([(0,0),(1,0),(0,1)], closed=True, transform=ax.transAxes)
im.set_clip_path(tri)
推荐阅读
- python-3.x - Tkinter 将 entry.get() 转换为整数问题
- php - PHP 文件夹结构:public/index.php 无法访问更高级别的文件
- python - 运行 Selenium geckodriver 而不生成 geckodriver 日志文件
- assembly - 如何在 8086 程序集中将 Word 转换为 Ascii 字符串?
- ruby-on-rails - Rails upsert 返回值无法创建实例 | ActiveRecord::AssociationType 不匹配
- reactjs - React - 我的自定义钩子在元素外点击有什么问题?
- javascript - Javascript - 通过删除服务持续时间来处理时隙
- python - 我无法使用 python datetime 更改日期格式
- apache-spark - 用于 Exchange 分区的 Spark 物理计划 false/true
- sql - 如何在数据库中具有相同名称的两个不同表中创建两个单独的外键列