matplotlib pyplot colorbar问题 [英] matplotlib pyplot colorbar question

查看:40
本文介绍了matplotlib pyplot colorbar问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,我正在尝试使用关联的颜色栏来绘制具有颜色的散点图.我希望颜色条具有字符串值而不是数字值,因为我正在比较两个不同的数据集,每个数据集具有不同的颜色值(但无论如何在最大值和最小值之间).这是我正在使用的代码

Dear all, I'm trying to perform a scatter plot with color with an associated color bar. I would like the colorbar to have string values rather than numerical values, as I'm comparing two different data sets each one with different colorvalues (but in any case between a maximum and minimum values). Here the code I'm using

import matplotlib.pyplot as plt
import numpy as np
from numpy import *
from matplotlib import rc
import pylab
from pylab import *
from matplotlib import mpl
data   = np.loadtxt('deltaBinned.txt')
data2  = np.loadtxt('deltaHalphaBinned.txt')
fig=plt.figure()
fig.subplots_adjust(bottom=0.1)
ax=fig.add_subplot(111)
plt.xlabel(r'$\partial \Delta/\partial\Phi[$mm$/^{\circ}]$',fontsize=16)
plt.ylabel(r'$\Delta$ [mm]',fontsize=16)
plt.scatter(data[:,0],data[:,1],marker='o',c=data[:,3],s=data[:,3]*1500,cmap=cm.Spectral,vmin=min(data[:,3]),vmax=max(data[:,3]))
plt.scatter(data2[:,0],data2[:,1],marker='^',c=data2[:,2],s=data2[:,2]*500,cmap=cm.Spectral,vmin=min(data2[:,2]),vmax=max(data2[:,2]))
cbar=plt.colorbar(ticks=[min(data2[:,2]),max(data2[:,2])])
cbar.set_ticks(['Low','High'])
cbar.set_label(r'PdF')
plt.show()

不幸的是,它不起作用,因为cbar.set_ticks不接受字符串值.我读过铃儿 http://matplotlib.sourceforge.net/examples/pylab_examples/colorbar_tick_labelling_demo.html 但我无法使其适应我的情况.如果问题很简单,我深表歉意,但我才刚刚开始 Python 编程尼古拉.

Unfortunately it does not work as cbar.set_ticks does not accept string values. I've read the ling http://matplotlib.sourceforge.net/examples/pylab_examples/colorbar_tick_labelling_demo.html but Iwas not able to adapt it to my case. I apologize if the question is simple but I'm just at the beginning of python programming Nicola.

推荐答案

cbar.ax.set_yticklabels(['Low','High'])

例如,

import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt

data = np.random.random((10, 4))
data2 = np.random.random((10, 4))
plt.subplots_adjust(bottom = 0.1)
plt.xlabel(r'$\partial \Delta/\partial\Phi[$mm$/^{\circ}]$', fontsize = 16)
plt.ylabel(r'$\Delta$ [mm]', fontsize = 16)
plt.scatter(
    data[:, 0], data[:, 1], marker = 'o', c = data[:, 3], s = data[:, 3]*1500,
    cmap = cm.Spectral, vmin = min(data[:, 3]), vmax = max(data[:, 3]))
plt.scatter(
    data2[:, 0], data2[:, 1], marker = '^', c = data2[:, 2], s = data2[:, 2]*500,
    cmap = cm.Spectral, vmin = min(data2[:, 2]), vmax = max(data2[:, 2]))
cbar = plt.colorbar(ticks = [min(data2[:, 2]), max(data2[:, 2])])
cbar.ax.set_yticklabels(['Low', 'High'])
cbar.set_label(r'PdF')
plt.show()

产生

这篇关于matplotlib pyplot colorbar问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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