Plot contour figure from data which has large scale and plus-minus difference with minimum limit using Python and matplotlib.pyplot


The result is:

Plot contour figure from data which has large scale and plus-minus difference with minimum limit using Python and matplotlib.pyplot

This page shows my suggestion to generate contour figure from data which has large scale and plus-minus difference using python and matplotlib.pyplot. The minimum value and maximum value can be specified in this code.

See also:

Python Matplotlib Tips: Plot 1D data which has large scale and plus-minus difference using Python and matplotlib.pyplot

This page shows my suggestion to generate 1D line figure from data which has large scale and plus-minus difference using python and matplotlib.pyplot.


Python Matplotlib Tips: Plot contour figure from data which has large scale and plus-minus difference using Python and matplotlib.pyplot

This page shows my suggestion to generate contour figure from data which has large scale and plus-minus difference using python and matplotlib.pyplot.



In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
xs = np.linspace(-2,2,100)
ys = np.linspace(-2,2,100)
XX,YY = np.meshgrid(xs,ys)
zs = 10**(XX*YY)
zs *= XX/np.abs(XX)

Original way to show this data

In [3]:
plt.figure(facecolor='w',figsize=(5,4))
plt.axes().set_aspect('equal', 'datalim')
plt.contourf(XX,YY,zs,100,cmap='jet')
cbar = plt.colorbar()
ctks = [-10000,-8000,-6000,-4000,-2000,0,2000,4000,6000,8000,10000]
cbar.set_ticks(ctks)
plt.savefig("pm_log_2d_1.png")

Convert raw data to log friendly data.
By introducing following calculation, values from -(10^CSHIFT) to 10^(CSHIFT) will be converted to 0.0.
You can change the mimimum value to be converted to zero by changing CSHIFT.

In [4]:
CSHIFT = -1
pm_flag = np.sign(zs)
zlog = np.abs(zs)
zlog *= 10**CSHIFT
zlog[np.where(zlog<1.0)] = 1.0
zlog = pm_flag * np.log10(zlog)

Improved way to show this data

In [5]:
cminp,cmaxp = 1,4 # min,max values of colorbar in plus side
cminm,cmaxm = 1,4 # min,max values of colorbar in minus side
plt.figure(facecolor='w',figsize=(5,4))
plt.axes().set_aspect('equal', 'datalim')
lvs = np.linspace(-cmaxm-CSHIFT,cmaxp+CSHIFT,100)

plt.contourf(XX,YY,zlog,100,cmap='jet',levels=lvs)
cbar = plt.colorbar()
# y tick values in log scale
ctks1,ctks2,ctks3 = np.arange(cminm+1,cmaxm+0.01),[0],np.arange(cminp+1,cmaxp+0.01)
# y tick positions to show in graph
ctkps = [-v-CSHIFT for v in ctks1[::-1]]+ctks2+[v+CSHIFT for v in ctks3]
# y tkck lavels in str
ctkls = ['$-10^{%d}$'%(v) for v in ctks1[::-1]]+['$-10$~$10$']+['$10^{%d}$'%(v) for v in ctks3]
# the label 0 should be in other way such as -1 to 1 or -1<v<1
cbar.set_ticks(ctkps)
cbar.set_ticklabels(ctkls)
plt.savefig("pm_log_2d_cut1.png")

You can adjust minimum and maximum value by changing cmaxm and cmaxp.

In [6]:
cminp,cmaxp = 1,4 # min,max values of colorbar in plus side
cminm,cmaxm = 1,3 # min,max values of colorbar in minus side
plt.figure(facecolor='w',figsize=(5,4))
plt.axes().set_aspect('equal', 'datalim')
lvs = np.linspace(-cmaxm-CSHIFT,cmaxp+CSHIFT,100)

plt.contourf(XX,YY,zlog,100,cmap='jet',levels=lvs)
cbar = plt.colorbar()
# y tick values in log scale
ctks1,ctks2,ctks3 = np.arange(cminm+1,cmaxm+0.01),[0],np.arange(cminp+1,cmaxp+0.01)
# y tick positions to show in graph
ctkps = [-v-CSHIFT for v in ctks1[::-1]]+ctks2+[v+CSHIFT for v in ctks3]
# y tkck lavels in str
ctkls = ['$-10^{%d}$'%(v) for v in ctks1[::-1]]+['$-10$~$10$']+['$10^{%d}$'%(v) for v in ctks3]
# the label 0 should be in other way such as -1 to 1 or -1<v<1
cbar.set_ticks(ctkps)
cbar.set_ticklabels(ctkls)
plt.savefig("pm_log_2d_cut2.png")