简体   繁体   English

修复matplotlib中散点图中的颜色

[英]Fixing color in scatter plots in matplotlib

I want to fix the color range on multiple scatter plots and add in a colorbar to each plot (which will be the same in each figure). 我想修复多个散点图上的颜色范围,并在每个图中添加一个颜色条(每个图中都是相同的)。 Essentially, I'm fixing all aspects of the axes and colorspace etc. so that the plots are directly comparable by eye. 基本上,我正在修复轴和颜色空间等的所有方面,以便绘图直接可以通过眼睛进行比较。

For the life of me, I can't seem to figure out all the various ways of fixing the color-range. 对于我的生活,我似乎无法弄清楚修复颜色范围的各种方法。 I've tried vmin, vmax, but it doesn't seem to do anything, I've also tried clim(x,y) and that doesn't seem to work either. 我已经尝试了vmin,vmax,但它似乎没有做任何事情,我也试过攀登(x,y),这似乎也没有用。

This must come up here and there, I can't be the only one that wants to compare various subsets of data amongst plots... so, how do you fix the colors so that each data keeps it's color between plots and doesn't get remapped to a different color due to the change in max/min of the subset -v- the whole set? 这必须在这里和那里出现,我不可能是唯一一个想要比较各个数据子集的人...所以,你如何修复颜色,以便每个数据保持它的颜色之间的颜色和不由于子集的最大/最小值的变化-v-整个集合,会重新映射到不同的颜色?

Setting vmin and vmax should do this. 设置vmin和vmax应该这样做。

Here's an example: 这是一个例子:

import matplotlib.pyplot as plt

xyc = range(20)

plt.subplot(121)
plt.scatter(xyc[:13], xyc[:13], c=xyc[:13], s=35, vmin=0, vmax=20)
plt.colorbar()
plt.xlim(0, 20)
plt.ylim(0, 20)

plt.subplot(122)
plt.scatter(xyc[8:20], xyc[8:20], c=xyc[8:20], s=35, vmin=0, vmax=20)   
plt.colorbar()
plt.xlim(0, 20)
plt.ylim(0, 20)

plt.show()

And the plot this produces: 这产生的情节:

替代文字

Ok, this isn't really an answer-but a follow-up. 好吧,这不是一个真正的答案 - 而是一个后续行动。 The results of my coding altering Tom's code above. 我的编码结果改变了Tom的代码。 [not sure that I want to remove the answer check-mark, as the code above does work, and is an answer to the question!] [不确定我是否要删除答案复选标记,因为上面的代码确实有效,并且是问题的答案!]

It doesn't appear to work for my data!! 它似乎不适用于我的数据!! Below is modified code that can be used with my data to produce a plot which wasn't working for me for some strange reason. 下面是修改后的代码,可以与我的数据一起使用,以产生一个因某些奇怪的原因而无法正常工作的情节。 The input came by way of the h5py functions (hdf5 data file import). 输入来自h5py函数(hdf5数据文件导入)。

In the below, rf85 is a subset of the arrays for the large batch of experiments where the RF power applied to the system was approximately 85 watts forward. 在下文中,rf85是用于大批量实验的阵列的子集,其中施加到系统的RF功率向前约为85瓦。 I'm basically slicing and dicing the data in various ways to try and see a trend. 我基本上是以各种方式切割和切割数据以尝试看到趋势。 This is the 85 watts compared to the full dataset that's current input (there's more data, but this is what I have for now). 与当前输入的完整数据集相比,这是85瓦特(有更多数据,但这就是我现在所拥有的)。

import numpy
import matplotlib.pyplot as plt

CurrentsArray = [array([ 0.83333333,  0.8       ,  0.57142857,  0.83333333,  1.03333333,
        0.25      ,  0.81666667,  0.35714286,  0.26      ,  0.57142857,
        0.83333333,  0.47368421,  0.80645161,  0.47368421,  0.52631579,
        0.36666667,  0.47368421,  0.57142857,  0.47368421,  0.47368421,
        0.47368421,  0.47368421,  0.47368421,  0.61764706,  0.81081081,
        0.41666667,  0.47368421,  0.47368421,  0.45      ,  0.73333333,
        0.8       ,  0.8       ,  0.8       ,  0.47368421,  0.45      ,
        0.47368421,  0.83333333,  0.47368421,  0.22222222,  0.32894737,
        0.57142857,  0.83333333,  0.83333333,  1.        ,  1.        ,
        0.46666667])]

growthTarray = [array([ 705.,  620.,  705.,  725.,  712.,  705.,  680.,  680.,  620.,
        660.,  660.,  740.,  721.,  730.,  720.,  720.,  730.,  705.,
        690.,  705.,  680.,  715.,  705.,  670.,  705.,  705.,  650.,
        725.,  725.,  650.,  650.,  650.,  714.,  740.,  710.,  717.,
        737.,  740.,  660.,  705.,  725.,  650.,  710.,  703.,  700.,  650.])]

CuSearray = [array([ 0.46395015,  0.30287259,  0.43496888,  0.46931773,  0.47685844,
        0.44894925,  0.50727844,  0.45076198,  0.44977095,  0.41455029,
        0.38089693,  0.98174953,  0.48600461,  0.65466528,  0.40563053,
        0.22990327,  0.54372179,  0.43143358,  0.92515847,  0.73701742,
        0.64152173,  0.52708783,  0.51794063,  0.49      ,  0.48878252,
        0.45119732,  0.2190089 ,  0.43470776,  0.43509758,  0.52697697,
        0.21576805,  0.32913721,  0.48828072,  0.62201997,  0.71442359,
        0.55454867,  0.50981136,  0.48212956,  0.46      ,  0.45732419,
        0.43402525,  0.40290777,  0.38594786,  0.36777306,  0.36517926,
        0.29880924])]

PFarray = [array([ 384.,  285.,  280.,  274.,  185.,  185.,  184.,  184.,  184.,
        184.,  184.,  181.,  110.,  100.,  100.,  100.,   85.,   85.,
         84.,   84.,   84.,   84.,   84.,   84.,   84.,   84.,   84.,
         84.,   84.,   84.,   84.,   84.,   27.,   20.,    5.,    5.,
          1.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.])]

rf85growthTarray = [array([ 730.,  705.,  690.,  705.,  680.,  715.,  705.,  670.,  705.,
        705.,  650.,  725.,  725.,  650.,  650.,  650.])]

rf85CuSearray = [array([ 0.54372179,  0.43143358,  0.92515847,  0.73701742,  0.64152173,
        0.52708783,  0.51794063,  0.49      ,  0.48878252,  0.45119732,
        0.2190089 ,  0.43470776,  0.43509758,  0.52697697,  0.21576805,
        0.32913721])]

rf85PFarray = [array([ 85.,  85.,  84.,  84.,  84.,  84.,  84.,  84.,  84.,  84.,  84.,
        84.,  84.,  84.,  84.,  84.])]

rf85CurrentsArray = [array([ 0.54372179,  0.43143358,  0.92515847,  0.73701742,  0.64152173,
        0.52708783,  0.51794063,  0.49      ,  0.48878252,  0.45119732,
        0.2190089 ,  0.43470776,  0.43509758,  0.52697697,  0.21576805,
        0.32913721])]

Datavmax = max(max(CurrentsArray))
Datavmin = min(min(CurrentsArray))

plt.subplot(121)
plt.scatter(growthTarray, CuSearray, PFarray, CurrentsArray, vmin=Datavmin, vmax=Datavmax, alpha=0.75)
plt.colorbar()
plt.xlim(600,760)
plt.ylim(0,2.5)

plt.subplot(122)
plt.scatter(rf85growthTarray, rf85CuSearray, rf85PFarray, rf85CurrentsArray, vmin=Datavmin, vmax=Datavmax, alpha=0.75)
plt.colorbar()
plt.xlim(600,760)
plt.ylim(0,2.5)

plt.show()

And finally, the output: 最后,输出:

colorfailed!

Please note that this is not the perfect output for my work, but I didn't expend effort making it perfect. 请注意,这不是我工作的完美输出,但我没有花费精力使其完美。 What is important however: datapoints that you'll recognize as the same between plots do not contain the same color as should be the case based on the vmin vmax use above (as Tom's code suggests). 然而重要的是:您在地块之间识别出相同的数据点不包含与基于上面使用的vmin vmax的情况相同的颜色 (如Tom的代码所示)。

This is insane. 疯了吧。 :( I do hope someone can shed light on this for me! I'm positive my code is not that great, so please don't worry about offending in anyway when it comes to my code!! :(我希望有人可以为我揭示这一点!我很肯定我的代码不是那么好,所以请不要担心在我的代码中冒犯了!

Extra bag of firey-hot cheetos to anyone who can suggest a way forward. 额外的一袋火热的cheetos给任何可以提出前进方向的人。 -Allen -Allen

UPDATE- Tom10 caught the problem - I had inadvertently used the wrong data for one of my sub-arrays, causing the values to give different color levels than expected (ie, my data was wrong!) Big props to Tom for this- I wish I could give him another up-vote, but due to my method of asking this question, I can't (sorry Tom!) 更新 - Tom10遇到了这个问题 - 我无意中为我的一个子阵列使用了错误的数据,导致这些值给出了不同于预期的颜色级别(即,我的数据错误了!)汤姆对此的大道具 - 我希望我可以给他另一个投票,但由于我提出这个问题的方法,我不能(对不起汤姆!)

Please also see his wonderful example of plotting text at the data positions mentioned below. 另请参阅他在下面提到的数据位置绘制文本的精彩示例。

Here's an updated image showing that Tom's method does indeed work, and that the plotting was a problem in my own code: 这是一个更新的图像,显​​示Tom的方法确实有效,并且绘图是我自己的代码中的一个问题: 替代文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM