zsh2270365028的个人博客分享 http://blog.sciencenet.cn/u/zsh2270365028

博文

[转载]关于python:matplotlib等高线图:对数刻度成比例的颜色条水平

已有 1901 次阅读 2022-3-10 21:08 |个人分类:Python|系统分类:科研笔记|文章来源:转载

refer to: https://www.codenong.com/18191867/

matplotlib contour plot: proportional colorbar levels in logarithmic scale

是否可以像下面的图像那样以对数刻度显示颜色条级别?

enter image description here

这是一些可以实现的示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
delta = 0.025

x = y = np.arange(0, 3.01, delta)
X, Y = np.meshgrid(x, y)
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 1e6 * (Z1* Z2)

fig=plt.figure()
ax1 = fig.add_subplot(111)
lvls = np.logspace(0,4,20)
CF = ax1.contourf(X,Y,Z,
         norm = LogNorm(),
         levels = lvls
        )
CS = ax1.contour(X,Y,Z,
         norm = LogNorm(),
         colors = 'k',
         levels = lvls
        )
cbar = plt.colorbar(CF, ticks=lvls, format='%.4f')
plt.show()

enter image description here

我在Windows 7上将python 2.7.3与matplotlib 1.1.1一起使用。

相关讨论

  • 您的颜色条已经具有对数刻度。

  • @nordev-我相信OP正在询问如何在颜色栏上设置刻度定位器和格式化程序,以便以规则的日志间隔显示标签。

  • @JoeKington嗯,我似乎想起来,他最初的问题中的OP希望将刻度线放在对数间距为lvls数组中给定的值上,但也许我只是误解了他的意思。 感谢您指出。

  • 您可能可以使用matplotlib.colors.LogNorm()来执行类似于此答案的操作


我建议生成一个伪彩色条,如下所示(有关说明,请参见注释):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
import matplotlib.gridspec as gridspec

delta = 0.025

x = y = np.arange(0, 3.01, delta)
X, Y = np.meshgrid(x, y)
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 1e6 * (Z1 * Z2)

fig=plt.figure()

#
# define 2 subplots, using gridspec to control the
# width ratios:
#
# note: you have to import matplotlib.gridspec for this
#
gs = gridspec.GridSpec(1, 2,width_ratios=[15,1])

# the 1st subplot
ax1 = plt.subplot(gs[0])

lvls = np.logspace(0,4,20)

CF = ax1.contourf(X,Y,Z,
                  norm = LogNorm(),
                  levels = lvls
                 )
CS = ax1.contour(X,Y,Z,
                 norm = LogNorm(),
                 colors = 'k',
                 levels = lvls
                )

#
# the pseudo-colorbar
#

# the 2nd subplot
ax2 = plt.subplot(gs[1])        

#
# new levels!
#
# np.logspace gives you logarithmically spaced levels -
# this, however, is not what you want in your colorbar
#
# you want equally spaced labels for each exponential group:
#
levls = np.linspace(1,10,10)
levls = np.concatenate((levls[:-1],np.linspace(10,100,10)))
levls = np.concatenate((levls[:-1],np.linspace(100,1000,10)))
levls = np.concatenate((levls[:-1],np.linspace(1000,10000,10)))

#
# simple x,y setup for a contourf plot to serve as colorbar
#
XC = [np.zeros(len(levls)), np.ones(len(levls))]
YC = [levls, levls]
CM = ax2.contourf(XC,YC,YC, levels=levls, norm = LogNorm())
# log y-scale
ax2.set_yscale('log')  
# y-labels on the right
ax2.yaxis.tick_right()
# no x-ticks
ax2.set_xticks([])

plt.show()

这会给你这样的情节:

pseudo-colorbar

编辑

或者,在调用colorbar时使用新级别和spacing='proportional'选项之类的东西:

您将得到以下情节:

real-colorbar

(仅更改了format,因为新的刻度不需要4位小数)

编辑2
如果您想自动生成类似我使用过的级别,可以考虑以下代码:

1
2
3
4
5

levels = []
LAST_EXP = 4
N_LEVELS = 5
for E in range(0,LAST_EXP):
    levels = np.concatenate((levels[:-1],np.linspace(10**E,10**(E+1),N_LEVELS)))

  • 替换此行:

    1

    lvls = np.logspace(0,4,20)

    用这些:

    1
    2
    3
    4

    lvls = np.linspace(1,10,5)
    lvls = np.concatenate((lvls[:-1],np.linspace(10,100,5)))
    lvls = np.concatenate((lvls[:-1],np.linspace(100,1000,5)))
    lvls = np.concatenate((lvls[:-1],np.linspace(1000,10000,5)))

  • 替换此行:

    1

    cbar = plt.colorbar(CF, ticks=lvls, format='%.4f')

    有了这个:

    1

    cbar = plt.colorbar(CF, ticks=lvls, format='%.2f', spacing='proportional')




https://blog.sciencenet.cn/blog-587102-1328876.html

上一篇:[转载]Python散点图绘制--Matplotlib详解
下一篇:arXiv:2022.4.1-4.10
收藏 IP: 119.78.226.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-19 20:46

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部