具有颜色渐变的3d散点图,其中颜色取决于计数 [英] 3d scatter plot with color gradient where color depends on count

查看:65
本文介绍了具有颜色渐变的3d散点图,其中颜色取决于计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧,其中的点包括该点的x,y和z坐标以及计数,每个数据点的数字在1到187之间。
我想将 count与颜色渐变相关联,其中1例如是绿色,而187是红色,然后绘制具有x,y和z坐标的数据点的散点图,其中每个颜色数据点是计数的编码信息。
我发现颜色渐变代码非常令人困惑,您能帮我吗?

I have dataframe with points which include x, y and z coordinate of the point and "count", which is number between 1 and 187 for each data point. I would like to associate "count" with color gradient, with 1 being for instance color green, and 187 color red, and then to make scatter plot of data points with x, y and z coordinate, where color of every data point is coded information for the "count". I find code for color gradients very confusing, can you please help me?

编辑:zelusp完全回答了我的问题,谢谢。

zelusp completely answered my question, thank you.

编辑1:我想知道是否应该编写单独的问题,但是由于它是关于同一段代码的,也许我只需编辑上一个问题就可以回答。
最近我更换了笔记本电脑,并在新计算机上安装了Ubuntu 18.04。这段代码可在Ubuntu 16.04上完美运行,但不适用于我的新计算机。
我的代码是:

I wondered if I should write separate question, but as it's about the same piece of code maybe I could reach for answer just editing previous question. Recently I changed my laptop, and installed Ubuntu 18.04 on my new machine. This piece of code worked perfectly on Ubuntu 16.04, but doesn't work on my new machine. My code is:

cmap = plt.cm.rainbow
norm = mpl.colors.Normalize(vmin=np.min(df3.h_count), vmax=np.max(df3.h_count))

fig=plt.figure()
ax1=fig.add_subplot(111, projection='3d')
ax1.scatter(df3.zm_bin, df3.sfr_bin, 12.+np.log10(df3.medijana), s=10, c=cmap(norm(df3.h_count)), marker='o') 
ax1.set_xlim(8,12.5)
ax1.set_xlabel('Log(Mz)')
ax1.set_ylabel('LogSFR') #treba invertovati
ax1.set_zlabel('12+log(Z)')
ax1.invert_yaxis()

sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
fig.colorbar(sm)

错误我得到的是这样的:

The error I'm getting is this:

Traceback (most recent call last):
  File "load_subhalos.py", line 233, in <module>
    ax1.scatter(df3.zm_bin, df3.sfr_bin, 12.+np.log10(df3.medijana), s=10, c=cmap(norm(df3.h_count)), marker='o') 
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/colors.py", line 938, in __call__
    result, is_scalar = self.process_value(value)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/colors.py", line 924, in process_value
    result = np.ma.array(data, mask=mask, dtype=dtype, copy=True)
  File "/usr/local/lib/python2.7/dist-packages/numpy/ma/core.py", line 6358, in array
    ndmin=ndmin, shrink=shrink, order=order)
  File "/usr/local/lib/python2.7/dist-packages/numpy/ma/core.py", line 2784, in __new__
    order=order, subok=True, ndmin=ndmin)
TypeError: float() argument must be a string or a number

我没有更改代码中的任何内容,唯一的变化是新机器和操作系统。您知道我为什么会遇到这个问题吗?

I didn't change anything in the code, the only change is new machine and OS. Do you know why I'm having this problem?

推荐答案

我建议您浏览一下matplotlib上的以下文章:

I recommend you take a tour through these posts on matplotlib:

  • Matplotlib 3D scatter plot with color gradient which is a near duplicate of your question
  • How to choose a good colormap I would advise you try to use the YlOrRd color map since it it will be a little easier to read
  • How can I convert numbers to a color scale in matplotlib? pay special attention to the use of the cmap, and norm variables and how they are used in cmap(norm(df.c.values)).

请牢记以下几点:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
from mpl_toolkits.mplot3d import Axes3D

#%% Generate mock data
number_of_datapoints = 30
x = np.random.rand(number_of_datapoints)
y = np.random.rand(number_of_datapoints)
z = np.random.rand(number_of_datapoints)

count_min = 1
count_max = 187
data = np.random.randint(count_min, count_max, number_of_datapoints) # these are your counts

#%% Create Color Map
colormap = plt.get_cmap("YlOrRd")
norm = matplotlib.colors.Normalize(vmin=min(data), vmax=max(data))

#%% 3D Plot
fig = plt.figure()
ax3D = fig.add_subplot(111, projection='3d')
ax3D.scatter(x, y, z, s=10, c=colormap(norm(data)), marker='o')  
plt.show()

您可能还对 colorspacious

这篇关于具有颜色渐变的3d散点图,其中颜色取决于计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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