Python 在mapplotlib中绘制热图的问题

Python 在mapplotlib中绘制热图的问题,python,pandas,matplotlib,seaborn,heatmap,Python,Pandas,Matplotlib,Seaborn,Heatmap,我有一个测试文件,它是我处理的一组数据的一小部分。这个特定的文件有25k行和3列。X位置、Y位置和能量沉积。我正在做一个对应于不同能量强度的热图。我对任何类型的计算机工作都非常陌生,所以这有点吓人 %matplotlib inline import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3

我有一个测试文件,它是我处理的一组数据的一小部分。这个特定的文件有25k行和3列。X位置、Y位置和能量沉积。我正在做一个对应于不同能量强度的热图。我对任何类型的计算机工作都非常陌生,所以这有点吓人

%matplotlib inline
import pandas as pd
import numpy as np
import seaborn as sns

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
这会给我以下错误消息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape, get_next_color_func)
   4284             try:  # Then is 'c' acceptable as PathCollection facecolors?
-> 4285                 colors = mcolors.to_rgba_array(c)
   4286                 n_elem = colors.shape[0]

~\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba_array(c, alpha)
    293     for i, cc in enumerate(c):
--> 294         result[i] = to_rgba(cc, alpha)
    295     return result

~\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba(c, alpha)
    176     if rgba is None:  # Suppress exception chaining of cache lookup failure.
--> 177         rgba = _to_rgba_no_colorcycle(c, alpha)
    178         try:

~\Anaconda3\lib\site-packages\matplotlib\colors.py in _to_rgba_no_colorcycle(c, alpha)
    239         # Test dimensionality to reject single floats.
--> 240         raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
    241     # Return a tuple to prevent the cached value from being modified.

ValueError: Invalid RGBA argument: (('-0.0101072', -7.0427), 11227    0.067631
Name: eDep, dtype: float64)

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-10-3ff2dded4a5c> in <module>
     16 fig = plt.figure(figsize = (6,5))
     17 ax = fig.add_subplot(1,1,1)
---> 18 s = ax.scatter('xPos', 'yPos', c = 'eDep',data = phase_1_2, cmap = 'Blues_r', marker = 's',s = 190)
     19 ax.axis([phase_1_2['xPos'].min()-10, phase_1_2['xPos'].max()+10, phase_1_2['yPos'].min()-10, phase_1_2['yPos'].max()+10])
     20 ax.set_xticks(major_ticks)

~\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1637                 bound.arguments.setdefault(varkwargs_name, {})["label"] = label
   1638 
-> 1639         return func(*bound.args, **bound.kwargs)
   1640 
   1641     inner.__doc__ = _add_data_doc(inner.__doc__, replace_names)

~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, **kwargs)
   4452             self._parse_scatter_color_args(
   4453                 c, edgecolors, kwargs, xshape, yshape,
-> 4454                 get_next_color_func=self._get_patches_for_fill.get_next_color)
   4455 
   4456         if plotnonfinite and colors is None:

~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape, get_next_color_func)
   4304                         "'c' argument must be a mpl color, a sequence of mpl "
   4305                         "colors or a sequence of numbers, not {}."
-> 4306                             .format(c)  # note: could be long depending on c
   4307                     )
   4308         else:

ValueError: 'c' argument must be a mpl color, a sequence of mpl colors or a sequence of numbers, not <pandas.core.groupby.generic.SeriesGroupBy object at 0x0000026797125808>.
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
~\Anaconda3\lib\site packages\matplotlib\axes\\u axes.py in\u parse\u scatter\u color\u args(c、edgecolors、kwargs、xshape、yshape、get\u next\u color\u func)
4284试试:#那么“c”作为PathCollection FaceColor是否可以接受?
->4285颜色=mcolors.to_rgba_数组(c)
4286 n_elem=颜色。形状[0]
~\Anaconda3\lib\site packages\matplotlib\colors.py插入到\u rgba\u数组(c,alpha)
293对于枚举(c)中的i,cc:
-->294结果[i]=至_rgba(cc,alpha)
295返回结果
~\Anaconda3\lib\site packages\matplotlib\colors.py in to_rgba(c,alpha)
176如果rgba为无:#抑制缓存查找失败的异常链接。
-->177 rgba=_至_rgba_无颜色循环(c,alpha)
178试试:
~\Anaconda3\lib\site packages\matplotlib\colors.py in\u to\u rgba\u no\u colorcycle(c,alpha)
239#测试维度以拒绝单个浮动。
-->240 raise VALUERROR(“无效的RGBA参数:{!r}.”格式(orig_c))
241#返回元组以防止修改缓存的值。
ValueError:无效的RGBA参数:(('-0.0101072',-7.0427),11227 0.067631
名称:eDep,数据类型:float64)
在处理上述异常期间,发生了另一个异常:
ValueError回溯(最近一次调用上次)
在里面
16图=零件图(图尺寸=(6,5))
17 ax=图add_子批次(1,1,1)
--->18 s=ax.scatter('xPos','yPos',c='eDep',data=phase_1_2,cmap='Blues_r',marker='s',s=190)
19最大轴([phase_1_2['xPos'].min()-10,phase_1_2['xPos'].max()+10,phase_1_2['yPos'].min()-10,phase_1_2['yPos'].max()+10])
20个轴设置刻度(主刻度)
内部的~\Anaconda3\lib\site packages\matplotlib\\uuuu init\uuuuuu.py(ax、数据、*args、**kwargs)
1637 bound.arguments.setdefault(varkwargs_name,{})[“label”]=label
1638
->1639返回函数(*bound.args,**bound.kwargs)
1640
1641内部.\uuuuu文档\uuuuu=\u添加\u数据\u文档(内部.\uuuuu文档\uuuuu,替换\u名称)
~\Anaconda3\lib\site packages\matplotlib\axes\\u axes.py散点(self、x、y、s、c、marker、cmap、norm、vmin、vmax、alpha、线宽、顶点、边色、plotnonfinite、**kwargs)
4452 self.\u parse\u scatter\u color\u args(
4453 c,edgecolors,kwargs,xshape,yshape,
->4454 get_next_color_func=self._get_补丁_用于填充。get_next_color)
4455
4456如果PLOTNONITE和colors为None:
~\Anaconda3\lib\site packages\matplotlib\axes\\u axes.py in\u parse\u scatter\u color\u args(c、edgecolors、kwargs、xshape、yshape、get\u next\u color\u func)
4304“'c'参数必须是mpl颜色,一个mpl序列”
4305“颜色或数字序列,而不是{}”
->4306.格式(c)#注意:根据c的不同,可能会很长
4307                     )
4308其他:
ValueError:“c”参数必须是mpl颜色、mpl颜色序列或数字序列,而不是。

非常感谢您的帮助!我也尝试过Seaborn,但当我旋转数据集时,它给了我一堆NaN值。

因此,从外观上看,问题在于数据的格式,您在这里分组:

phase_1_2=columns.groupby(['xPos','yPos'])
但你从来没有真正做过任何事情,eDep的结果。改为:

phase_1_2=columns.groupby(['xPos','yPos']).sum()
将为您提供eDep列的
sum
,并且由于您在
xPos
yPos
上分组,熊猫知道如何使用它们进行分组


您可以在此处阅读有关pandas groupBys的更多信息:

要分散的参数x和y必须是类似数组的对象,而不是字符串。。。可能是通过
df[“xPos”]
而不是
“xPos”
@kpie刚刚尝试了这个,给了我完全相同的错误:(
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape, get_next_color_func)
   4284             try:  # Then is 'c' acceptable as PathCollection facecolors?
-> 4285                 colors = mcolors.to_rgba_array(c)
   4286                 n_elem = colors.shape[0]

~\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba_array(c, alpha)
    293     for i, cc in enumerate(c):
--> 294         result[i] = to_rgba(cc, alpha)
    295     return result

~\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba(c, alpha)
    176     if rgba is None:  # Suppress exception chaining of cache lookup failure.
--> 177         rgba = _to_rgba_no_colorcycle(c, alpha)
    178         try:

~\Anaconda3\lib\site-packages\matplotlib\colors.py in _to_rgba_no_colorcycle(c, alpha)
    239         # Test dimensionality to reject single floats.
--> 240         raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
    241     # Return a tuple to prevent the cached value from being modified.

ValueError: Invalid RGBA argument: (('-0.0101072', -7.0427), 11227    0.067631
Name: eDep, dtype: float64)

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-10-3ff2dded4a5c> in <module>
     16 fig = plt.figure(figsize = (6,5))
     17 ax = fig.add_subplot(1,1,1)
---> 18 s = ax.scatter('xPos', 'yPos', c = 'eDep',data = phase_1_2, cmap = 'Blues_r', marker = 's',s = 190)
     19 ax.axis([phase_1_2['xPos'].min()-10, phase_1_2['xPos'].max()+10, phase_1_2['yPos'].min()-10, phase_1_2['yPos'].max()+10])
     20 ax.set_xticks(major_ticks)

~\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1637                 bound.arguments.setdefault(varkwargs_name, {})["label"] = label
   1638 
-> 1639         return func(*bound.args, **bound.kwargs)
   1640 
   1641     inner.__doc__ = _add_data_doc(inner.__doc__, replace_names)

~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, **kwargs)
   4452             self._parse_scatter_color_args(
   4453                 c, edgecolors, kwargs, xshape, yshape,
-> 4454                 get_next_color_func=self._get_patches_for_fill.get_next_color)
   4455 
   4456         if plotnonfinite and colors is None:

~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape, get_next_color_func)
   4304                         "'c' argument must be a mpl color, a sequence of mpl "
   4305                         "colors or a sequence of numbers, not {}."
-> 4306                             .format(c)  # note: could be long depending on c
   4307                     )
   4308         else:

ValueError: 'c' argument must be a mpl color, a sequence of mpl colors or a sequence of numbers, not <pandas.core.groupby.generic.SeriesGroupBy object at 0x0000026797125808>.