matplotlib中颜色栏刻度标签的旋转 [英] Rotation of colorbar tick labels in matplotlib

查看:155
本文介绍了matplotlib中颜色栏刻度标签的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想旋转颜色栏刻度标签,以便它们垂直读取而不是水平读取.我已经尝试过使用 cbar.ax.set_xticklabels cbar.ax.ticklabel_format 等尽可能多的变体,并使用 rotation ='vertical',但尚未完全着陆.

I would like to rotate the colorbar tick labels so that they read vertically rather than horizontally. I have tried as many variations as I can think of with cbar.ax.set_xticklabels and cbar.ax.ticklabel_format and so on with rotation='vertical' but haven't quite landed it yet.

我在下面提供了MWE:

I've provided a MWE below:

import numpy as np
import matplotlib.pyplot as plt

#example function
x,y = np.meshgrid(np.linspace(-10,10,200),np.linspace(-10,10,200))
z = x*y*np.exp(-(x+y)**2)

#array for contourf levels
clevs = np.linspace(z.min(),z.max(),50)

#array for colorbar tick labels
clevs1 =np.arange(-200,100,10)

cs1 = plt.contourf(x,y,z,clevs)

cbar = plt.colorbar(cs1, orientation="horizontal")
cbar.set_ticks(clevs1[::1])

plt.show()

任何指针将不胜感激-我相信这一定很简单...

Any pointers would be greatly appreciated - I'm sure this must be pretty simple...

推荐答案

您可以使用 cbar.ax.set_xticklabels 更改旋转角度(或者,如果您有垂直颜色栏).

You can use cbar.ax.set_xticklabels to change the rotation (or set_yicklabels if you had a vertical colorbar).

cbar.ax.set_xticklabels(clevs1[::1],rotation=90)

要正确设置刻度线,您可以使用 np.argmin 搜索 clevs1 数组中第一个刻度线的位置,并使用它来索引当您 set_xticklabels :

To set the ticks correctly, you can search for where in your clevs1 array the first tick should be using np.argmin, and use that to index clevs1 when you set_xticklabels:

tick_start = np.argmin(abs(clevs1-clevs[0]))
cbar.ax.set_xticklabels(clevs1[tick_start:],rotation=90)

这篇关于matplotlib中颜色栏刻度标签的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆