【问题标题】:How to invert the lengend from Max to Min [duplicate]如何将图例从 Max 反转为 Min [重复]
【发布时间】:2020-10-22 01:50:29
【问题描述】:

这是网络上的一个例子https://matplotlib.org/gallery/axes_grid1/simple_colorbar.html#sphx-glr-gallery-axes-grid1-simple-colorbar-py

我想反转图例,让它看起来像这样:

这意味着图例中底部的最大值和顶部的最小值。 使颜色条“镜像”

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10, 10)))

# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

plt.colorbar(im, cax=cax)

plt.show()

【问题讨论】:

  • 你还想要从上到下运行0-100的数字吗?
  • 如果您在ax.imshow() 中添加此选项cmap="viridis_r",您将获得所需的颜色条。但数字标签保持不变。可以吗。
  • 希望数字从上到下可以是0到100。对不起,我的陈述不清楚。

标签: python matplotlib


【解决方案1】:

您可以访问彩条的ax,然后调用invert_yaxis()。这将反转颜色条的颜色和刻度标签。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10, 10)))

# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

plt.colorbar(im, cax=cax)
cax.invert_yaxis()

plt.show()

PS:如果没有提供颜色条的ax,你可以访问它

cbar = plt.colorbar(im)
cbar.ax.invert_yaxis()

【讨论】:

  • 当然,你已经有了颜色条的轴,在这个例子中称为cax,所以可以使用它
猜你喜欢
  • 2017-09-23
  • 2019-11-11
  • 2017-06-10
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
相关资源
最近更新 更多