matplotlib 对称图_Matplotlib Contourf图中的对称对数色标

如何创建一个包含对称对数(symlog)尺度的等高线图,即适用于正负值的对数尺度。可以使用ticker.SymmetricalLogLocator(),但其文档不全。最终解决方案是通过ticker.MaxNLocator(nbins)实现细粒度的颜色标度,并结合Latex风格的刻度格式化,以展示大量正负数值的2D等高线图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How do I create a contour plot with a symlog (symmetrical log) scale for the contours. i.e. a log scale that shows both negative and positive values.

One possibility would be to work off of this example:

Which gives this recipe for a log scale:

from matplotlib import pyplot, ticker

cs = pyplot.contourf(X, Y, z, locator=ticker.LogLocator())

However, this doesn't allow for negative values. There is a ticker.SymmetricalLogLocator(), which may be the solution, but it doesn't seem to have much documentation.

EDIT:

To clarify (since requesting negative values on a log scale may sound nonsensical), what I want is the same as the "symlog" scale provided on matplotlib axes. The plot below, (taken from another stack exchange post), shows symlog on the x-axis. It is a "log" scale, but handles negative values in a way that is clear to the viewer.

I want the same sort of scaling, but for the colorscale on contour or contourf.

解决方案

I stumbled across this thread trying to do the same thing, i.e plotting a large range of values in both the positive and negative direction. In addition I wanted to have a granularity as fine as in imshow.

It turns out you can have that using "ticker.MaxNLocator(nbins)" where nbins can be set high to have a fine granularity, e.g. set nbins to 100.

I also wanted to have a nice Latex style ticker formatting, for which I found a solution on StackOverflow a while ago.

I will just post this code snippet here from one of the classes it is part of so that anyone who might want can get the basic idea about how it's working. I use this solution to generate multiple plots as shown in the image below.

import matplotlib.pyplot as plt

import matplotlib.ticker as ticker

# function for nice Latex style tick formatting

# copied from

# http://stackoverflow.com/questions/25983218/

# scientific-notation-colorbar-in-matplotlib

# output formating for colorbar in 2D plots

def fmt(x, pos):

a, b = '{:.2e}'.format(x).split('e')

b = int(b)

return r'${} \times 10^{{{}}}$'.format(a, b)

# A confourf function I use inside one of my classes

# mainly interesting are the "plot" and "cbar" lines

def Make2DSubPlot(self, posIdent, timeIdx,typeIdx):

plt.subplot(posIdent)

y = self.radPos

x = self.axPos

z = self.fieldList[timeIdx][typeIdx]

plot = plt.contourf(x, y, z, locator=ticker.MaxNLocator(100), \

aspect='auto',origin='lower')

cbar = plt.colorbar(plot, orientation='vertical', \

format=ticker.FuncFormatter(fmt))

cbar.ax.set_ylabel(self.labelList[typeIdx])

plt.xlabel(self.labelList[self.iax])

plt.ylabel(self.labelList[self.iax])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值