【问题标题】:Positioning colorbar and second y-axis (matplotlib)定位颜色条和第二个 y 轴(matplotlib)
【发布时间】:2019-12-06 11:04:43
【问题描述】:

我正在尝试制作一个带有双 y 轴和旁边的颜色条的图。但是,我的第二个 y 轴的标签不会出现,并且我在正确定位颜色条时遇到了一些麻烦。我的代码可以在下面找到:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm #colormap
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.transforms import Transform
from matplotlib.ticker import (
    AutoLocator, AutoMinorLocator)

x = np.linspace(-2.5, 2.5, 100)
y = np.linspace(-2.5, 2.5, 100)
xx, yy = np.meshgrid(x, y)

std_x = 1.
std_y = 1.
A = 10
Gaussian = A * np.exp(-(xx**2/(2*std_x**2) + yy**2/(2*std_y**2)))

fig = plt.figure()
ax = plt.subplot(111)
im = plt.imshow(Gaussian)
ax.set_xlabel("[m]")
ax.set_ylabel("[m]")
ax_new = ax.twinx().twiny()
ticks = np.linspace(0, 1, 5)
ticklabels = np.round(np.arctan(ticks*512/1e3)*3600) #hardcoded
plt.xticks(ticks, ticklabels)
plt.yticks(ticks, ticklabels)
ax_new.set_xlabel("arcsec")
ax_new.set_ylabel("arcsec")
ax_new.set_title("Atmosphere Structure", fontsize=20)
divider = make_axes_locatable(ax_new)
# on the figure total in percent [left, bottom, width, height]
cax = fig.add_axes([0.95, 0.1, 0.02, 0.75])
colorbar_1 = fig.colorbar(im, cax=cax)
colorbar_1.set_label('pwv in mm', labelpad=-10, y=1.05, rotation=0)
plt.show()

经过大量微调,现在的情节看起来像这样(为了简单起见,我使用了高斯):

颜色条仍然略微未对齐,我现在定位颜​​色条的方式感觉像是一种快速修复,而不是一个好的解决方案。有谁知道如何以“更清洁”/更好的方式做到这一点?有人知道如何让右边的 ylabel 出现(ax_new.set_ylabel("arcsec"))吗?

我尝试了Plot multiple y-axis AND colorbar in matplotlib 中提出的解决方案,但不知何故它对我不起作用。

编辑: 当我将cax = fig.add_axes([0.95, 0.1, 0.02, 0.75]) 替换为cax = divider.append_axes("right", size="4%", pad="10%") 时,我得到以下图像:

编辑2:

我也尝试过使用secondary_xaxis,可以在下面的代码中看到

def m2deg(x):
    arcsec = np.arctan(x*512/1e3)*180/np.pi
    return arcsec

def deg2m(x):
    m = np.tan(x/(512)*np.pi/180)*1e3
    return m

secax = ax.secondary_xaxis('top', functions=(m2deg, deg2m))
secax2 = ax.secondary_yaxis('right', functions=(m2deg, deg2m))
secax.set_label("degrees")
secax2.set_label("degrees")

它给了我下面的图片:

再次,轴标签不显示,刻度线没有正确的值(tan(0/(512)*pi/180)*1e3 = 0,arctan(0*512/1e3)*180 /pi = 0,所以所有的 0 必须对齐)。

【问题讨论】:

  • 感谢您的评论。我已经尝试过这种方式并编辑了问题以显示如果我尝试会发生什么。你知道为什么它看起来这么奇怪吗?
  • 哦,是的。由于您创建了 3 个轴(原始轴、孪生轴和孪生轴),因此每个轴都需要一个分隔器。看起来相当麻烦。您是否尝试过使用 secondary_xaxis 而不是 twinx ?
  • 我也试过了!但不知何故,它没有正确计算刻度线。您可以在我的新编辑中看到它。例如,两个轴上的 0 未对齐,从 m 到度时不应出现这种情况(请参阅我的代码中的 m2deg 函数)
  • 当然,因为你伪造了标签。使用 extent 关键字以要转换的原始单位绘制图像。

标签: python matplotlib plot axis-labels colorbar


【解决方案1】:

试试constrained_layout

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm #colormap

x = np.linspace(-2.5, 2.5, 100)
y = np.linspace(-2.5, 2.5, 100)
xx, yy = np.meshgrid(x, y)

std_x = 1.
std_y = 1.
A = 10
Gaussian = A * np.exp(-(xx**2/(2*std_x**2) + yy**2/(2*std_y**2)))

fig, ax = plt.subplots(constrained_layout=True)
im = ax.imshow(Gaussian)
ax_new = ax.twinx().twiny()
ticks = np.linspace(0, 1, 5)
ticklabels = np.round(np.arctan(ticks*512/1e3)*3600) #hardcoded
plt.xticks(ticks, ticklabels)
plt.yticks(ticks, ticklabels)
ax_new.set_xlabel("arcsec")
ax_new.set_ylabel("arcsec")
ax_new.set_title("Atmosphere Structure", fontsize=20)
colorbar_1 = fig.colorbar(im)
colorbar_1.set_label('pwv in mm', labelpad=-10, y=1.05, rotation=0)
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-23
    • 2021-05-14
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 2019-04-05
    • 2021-11-07
    相关资源
    最近更新 更多