在matplotlib中绘制多个y轴和颜色栏 [英] Plot multiple y-axis AND colorbar in matplotlib

查看:81
本文介绍了在matplotlib中绘制多个y轴和颜色栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个散点图,该散点图具有两个不同的y轴和一个色条.

I am trying to produce a scatter plot that has two different y-axes and also a colorbar.

这是使用的伪代码:

#!/usr/bin/python

import matplotlib.pyplot as plt
from matplotlib import cm

fig = plt.figure()
ax1 = fig.add_subplot(111)
plt.scatter(xgrid,
        ygrid,
        c=be,                   # set colorbar to blaze efficiency
        cmap=cm.hot,
        vmin=0.0,
        vmax=1.0)

cbar = plt.colorbar()
cbar.set_label('Blaze Efficiency')

ax2 = ax1.twinx()
ax2.set_ylabel('Wavelength')

plt.show()

它产生了这个情节:

我的问题是,您如何使用不同的比例来表示波长"?轴,还有,如何将颜色条向右移动更多,以使其不妨碍波长?

My question is, how do you use a different scale for the "Wavelength" axes, and also, how do you move the colorbar more to right so that it is not in the Wavelength's way?

推荐答案

@ OZ123很抱歉,我花了很长时间回复.Matplotlib具有可扩展的可定制性,有时甚至使您对实际操作感到困惑.感谢您在创建单独轴方面的帮助.

@OZ123 Sorry that I took so long to respond. Matplotlib has extensible customizability, sometimes to the point where you get confused to what you are actually doing. Thanks for the help on creating separate axes.

但是,我认为我并不需要那么多控制,最终我只在

However, I didn't think I needed that much control, and I ended up just using the PAD keyword argument in

fig.colorbar()

这提供了我所需要的.

然后,伪代码变为:

#!/usr/bin/python

import matplotlib.pyplot as plt
from matplotlib import cm

fig = plt.figure()
ax1 = fig.add_subplot(111)
mappable = ax1.scatter(xgrid,
                       ygrid,
                       c=be,                   # set colorbar to blaze efficiency
                       cmap=cm.hot,
                       vmin=0.0,
                       vmax=1.0)

cbar = fig.colorbar(mappable, pad=0.15)
cbar.set_label('Blaze Efficiency')

ax2 = ax1.twinx()
ax2.set_ylabel('Wavelength')

plt.show()

这里是要显示现在的样子::

Here is to show what it looks like now::

这篇关于在matplotlib中绘制多个y轴和颜色栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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