Matplotlib色条在左侧/相反侧打勾 [英] Matplotlib colorbar ticks on left/opposite side

查看:111
本文介绍了Matplotlib色条在左侧/相反侧打勾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人可能会像这样(简化)生成一个垂直的颜色条:

One could generate a vertical colorbar like so(simplified):

import matplotlib.pyplot as plt
import matplotlib as mpl

plt.figure()
c_ax=plt.subplot(111)
cb = mpl.colorbar.ColorbarBase(c_ax,orientation='vertical')
plt.savefig('my_colorbar.png')

结果如下(由于空间原因而旋转):

Resulting in something like this(rotated for space reasons):

是否可以在对面放置刻度线标签?

Is it possible to get the ticks labels on the opposite side?

推荐答案

您可以使用c_ax.yaxis.set_ticks_position()

以您的示例为例:

import matplotlib.pyplot as plt
import matplotlib as mpl

plt.viridis()

fig=plt.figure()
c_ax=plt.subplot(199)

cb = mpl.colorbar.ColorbarBase(c_ax,orientation='vertical')

c_ax.yaxis.set_ticks_position('left')

plt.savefig('my_colorbar.png')

请注意,您也可以通过类似的方式移动颜色栏标签:

Note you can also move the colorbar label in a similar way:

c_ax.yaxis.set_label_position('left')

最后,如果您有水平色条,并且想要将刻度和标签移到顶部,则可以使用xaxis上的等效功能:

And finally, in case you have a horizontal colorbar, and want to move the ticks and labels to the top, you can use the equivalent function on the xaxis:

c_ax.xaxis.set_label_position('top')
c_ax.xaxis.set_ticks_position('top')

这篇关于Matplotlib色条在左侧/相反侧打勾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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