【问题标题】:Issue ploting heatmap in mapplotlib在 matplotlib 中绘制热图问题
【发布时间】:2020-06-20 17:51:08
【问题描述】:

我有一个测试文件,其中包含我正在使用的一堆数据的一小部分。这个特定的文件是 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
AT = "./Analysis.Test.csv"
df = pd.read_csv(AT, sep = ',', names=['Name','TrackID','ParentID','xPos','yPos','zPos','eDep','DeltaE','Einit','EventID'],low_memory=False,error_bad_lines=False)
df["xPos"] = df["xPos"].str.replace("(","")
df["zPos"] = df["zPos"].str.replace(")","")

df.sort_values(by='Name', ascending=[False])
df.dropna(how='any',axis=0,subset=['Name','TrackID','ParentID','xPos','yPos','zPos','eDep','DeltaE','Einit','EventID'], inplace=True)
couple_columns=df[['xPos','yPos','eDep']]
phase_1_2 = couple_columns.groupby(['xPos', 'yPos'])
phase_1_2.head(10)


major_ticks = np.arange(-200, 200, 20)                                              
minor_ticks = np.arange(-200, 200, 5)  

fig = plt.figure(figsize = (6,5))  
ax = fig.add_subplot(1,1,1) 
s = ax.scatter('xPos', 'yPos', c = 'eDep',data = phase_1_2, cmap = 'Blues_r', marker = 's',s = 190)
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])
ax.set_xticks(major_ticks)                                                       
ax.set_xticks(minor_ticks, minor=True)                                           
ax.set_yticks(major_ticks)                                                                                                                                                      
ax.grid(which='both', alpha = 0.3)                                                                                                           
ax.grid(which='major', alpha=0.3) 
ax.set_xlabel('x position', fontsize=10);
ax.set_ylabel('y position', fontsize=10);
ax.set_title('Energy from Betas', size = 15)

cbar = plt.colorbar(mappable = s,ax = ax)

plt.show()

然后给我以下错误消息:

---------------------------------------------------------------------------
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>.

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

【问题讨论】:

  • 要分散的参数 x 和 y 必须是类似对象的数组,而不是字符串...也许通过 df["xPos"] 而不是 "xPos"
  • @kpie 刚刚尝试过,给了我完全相同的错误:(

标签: python pandas matplotlib seaborn heatmap


【解决方案1】:

所以从这里看,问题是与数据的格式有关,你在这里分组:

phase_1_2 = couple_columns.groupby(['xPos', 'yPos'])

但是你实际上从来没有对 eDep 做任何事情。改为:

phase_1_2 = couple_columns.groupby(['xPos', 'yPos']).sum()

会给你 eDep 列的 sum,因为你在 xPosyPos 上进行分组,pandas 知道使用它们进行分组。

您可以在此处阅读有关 pandas groupBys 的更多信息:https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html

【讨论】:

    猜你喜欢
    • 2020-03-31
    • 2016-01-21
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 2017-04-23
    • 2013-07-19
    • 1970-01-01
    相关资源
    最近更新 更多