matplotlib pyplot 颜色条问题

发布于 2024-09-25 17:24:59 字数 1269 浏览 0 评论 0原文

亲爱的大家,我正在尝试使用带有相关颜色条的颜色来执行散点图。我希望颜色栏具有字符串值而不是数值,因为我正在比较两个不同的数据集,每个数据集具有不同的颜色值(但在任何情况下都在最大值和最小值之间)。这里我使用的代码

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 编程 尼古拉.

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}]

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.

,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()

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡 2024-10-02 17:24:59
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}]

生成

alt text

, 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()

生成

alt text

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

For example,

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}]

produces

alt text

, 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()

produces

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文