如何用 Matplotlib imshow 画矩阵数据
source link: https://iphyer.github.io/blog/2020/07/01/imshowofMatirx/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
最近在研究 two time correlation function, 所以很多时候需要处理在 K 空间下的不同时间的关联函数,其中一个小点,我也是第一次发现,虽然只是小的参数选择,但是如果没有留意很容易张冠李戴得到完全相反的结果。这个帖子记录下自己的结果,希望也帮助大家可视化矩阵数据。
Matplotlib imshow default
通常在 Python 中,我们常用 Matplotlib 做可视化,画矩阵数据最常用的就是 imshow
函数. imshow
函数用官方文档的说法就是对 2D 数据做可视化的。
Display data as an image; i.e. on a 2D regular raster.
The input may either be actual RGB(A) data, or 2D scalar data, which will be rendered as a pseudocolor image.
所以往往我们会这样可视化,
import numpy as np
import matplotlib.pyplot as plt
# create testing data which is 4x5 data
mat = np.arange(20).reshape(4,5)
print(mat)
# Save Image Function
fig = plt.figure(figsize=(10,8))
ax = plt.gca()
cax = plt.imshow(mat, cmap='viridis')
# set up colorbar
cbar = plt.colorbar(cax, extend='both', drawedges = False)
cbar.set_label('Intensity',size=36, weight = 'bold')
cbar.ax.tick_params( labelsize=18 )
cbar.minorticks_on()
# set up axis labels
ticks=np.arange(0,mat.shape[0],1)
## For x ticks
plt.xticks(ticks, fontsize=12, fontweight = 'bold')
ax.set_xticklabels(ticks)
## For y ticks
plt.yticks(ticks, fontsize=12, fontweight = 'bold')
ax.set_yticklabels(ticks)
plt.savefig('test.png', dpi = 300)
plt.close()
具体效果如下图,
但是这个图在很多时候其实并不是我们想要得,以 two time correlation function 为例,这个图最大的问题是它的 origin point 不对。我们希望的 origin 在左下角,但是这个图在左上角。比如常见的 two time correlation function 如下图所示,来自 On the use of two-time correlation functions for X-ray photon correlation spectroscopy data analysis
Matplotlib imshow lower
那么如何作出正确的图呢?
其实很简单,只需要改动一行代码,
cax = plt.imshow(mat, cmap='viridis', origin = 'lower')
origin
参数就是控制在左上还是在作下显示原点的参数。
origin{‘upper’, ‘lower’}, optional
Place the [0, 0] index of the array in the upper left or lower left corner of the axes. The convention ‘upper’ is typically used for matrices and images. If not given, rcParams[“image.origin”] (default: ‘upper’) is used, defaulting to ‘upper’.
Note that the vertical axes points upward for ‘lower’ but downward for ‘upper’.
上面就是这个参数的详细官方解释,其实官方还给了一个非常给力的教程,大家可以参考 origin and extent in imshow
所以我们改动后的代码效果图如下,
import numpy as np
import matplotlib.pyplot as plt
# create testing data which is 4x5 data
mat = np.arange(20).reshape(4,5)
print(mat)
# Save Image Function
fig = plt.figure(figsize=(10,8))
ax = plt.gca()
cax = plt.imshow(mat, cmap='viridis', origin = 'lower')
# set up colorbar
cbar = plt.colorbar(cax, extend='both', drawedges = False)
cbar.set_label('Intensity',size=36, weight = 'bold')
cbar.ax.tick_params( labelsize=18 )
cbar.minorticks_on()
# set up axis labels
ticks=np.arange(0,mat.shape[0],1)
## For x ticks
plt.xticks(ticks, fontsize=12, fontweight = 'bold')
ax.set_xticklabels(ticks)
## For y ticks
plt.yticks(ticks, fontsize=12, fontweight = 'bold')
ax.set_yticklabels(ticks)
plt.savefig('test.png', dpi = 300)
plt.close()
这个帖子参考了这个 Stackoverflow 的讨论 Matplotlib imshow: Data rotated?
Recommend
-
23
在 Matplotlib 教程中,我们将讨论一些可能的图表自定义。 为了开始修改子图,我们必须定义它们。 我们很快会谈论他们,但有两种定义并构造子图的主要方法。 现在,我们只使用其中一个,但我们会很快解释它们。 现在,修改我们的...
-
27
作者|Rizky Maulana Nurhidayat 编译|VK 来源|Towards Datas Science 数据可视化用于以更直接的表示方式显示数据,并且更易于理解。它可以用柱状图、散点图、折线图、饼图等形式形成。许多人仍然使用Matplotli...
-
11
这篇文章笔记很多,注释貌似比代码多,毕竟第一次学习,写的详细一些吧,看不懂的代码多看注释 很久之前就想学习数据分析了,于是准备7天精通一个职业,虽然我不拿这七天学的东西去面试,但是做个总结吧,有兴趣的...
-
14
↑↑↑关注后" 星标 "Datawhale 每日干货 &
-
7
❤️数据科学-Pandas、Numpy、Matplotlib秘籍之精炼总结 ...
-
13
Matplotlib 的 pcolor 函数能够绘制由一个个四边形(quadrilateral)单元构成的网格数据的彩色图像,相比绘制等值填色图的 contourf 函数,pcolor 对网格的要求更宽松,并且不会对空间离散的变量产生虚假的平滑,因而在...
-
3
-
6
如何用BBDO的RAISE线性矩阵去写一篇卖货文案? 某天,我突然接到一个委托, br...
-
7
本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 大家好我是费老师,matpl...
-
4
数据可视化是数据分析非常关键的一部分,它可以帮助我们以直观的方式理解复杂的数据和模式。Matplotlib库因其灵活性和易用性而在Python社区中广受欢迎。接下来,我们将通过一些简单的示例来说明如何使用Matplotlib绘制不同类型的图表。安装Matplotli...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK