【问题标题】:How to plot multiple groups in different colors and shapes with matplotlib?如何使用 matplotlib 绘制不同颜色和形状的多个组?
【发布时间】:2015-07-19 10:23:20
【问题描述】:

给定以下 DataFrame(在 pandas 中):

        X    Y    Type   Region
 index
 1      100  50   A      US
 2      50   25   A      UK
 3      70   35   B      US
 4      60   40   B      UK
 5      80   120  C      US
 6      120  35   C      UK

为了生成DataFrame:

import pandas as pd

data = pd.DataFrame({'X': [100, 50, 70, 60, 80, 120],
                     'Y': [50, 25, 35, 40, 120, 35],
                     'Type': ['A', 'A', 'B', 'B', 'C', 'C'],
                     'Region': ['US', 'UK'] * 3
                    },
                    columns=['X', 'Y', 'Type', 'Region']
       )

我尝试制作XY 的散点图,颜色为Type,形状为Region。如何在 matplotlib 中实现它?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    更多熊猫:

    from pandas import DataFrame
    from matplotlib.pyplot import show, subplots 
    from itertools import cycle # Useful when you might have lots of Regions
    
    data = DataFrame({'X': [100, 50, 70, 60, 80, 120],
                         'Y': [50, 25, 35, 40, 120, 35],
                         'Type': ['A', 'A', 'B', 'B', 'C', 'C'],
                         'Region': ['US', 'UK'] * 3
                        },
                        columns=['X', 'Y', 'Type', 'Region']
           )
    
    cs = {'A':'red',
          'B':'blue',
          'C':'green'}
    
    markers = ('+','o','>') 
    fig, ax = subplots()
    
    for region, marker in zip(set(data.Region),cycle(markers)):
        reg_data = data[data.Region==region]
        reg_data.plot(x='X', y='Y',
              kind='scatter',
              ax=ax,
              c=[cs[x] for x in reg_data.Type],
              marker=marker,
              label=region)
    ax.legend()
    show()
    

    不过,对于这种多维情节,请查看 seaborn(适用于 pandas)。

    【讨论】:

      【解决方案2】:

      一种方法是执行以下操作。它不优雅,但有效 将 matplotlib.pyplot 导入为 plt 将 matplotlib 导入为 mpl 将 numpy 导入为 np plt.ion()

      colors  = ['g', 'r', 'c', 'm', 'y', 'k', 'b'] 
      markers = ['*','+','D','H']
      for iType in range(len(data.Type.unique())):
          for iRegion in range(len(data.Region.unique())):
              plt.plot(data.X.values[np.bitwise_and(data.Type.values   == data.Type.unique()[iType],
                                                    data.Region.values == data.Region.unique()[iRegion])],
                       data.Y.values[np.bitwise_and(data.Type.values   == data.Type.unique()[iType],
                                                    data.Region.values == data.Region.unique()[iRegion])],
                       color=colors[iType],marker=markers[iRegion],ms=10)
      

      我不熟悉 Panda,但必须有一些更优雅的方法来进行过滤。标记列表可以使用matplotlib中的markers.MarkerStyle.markers.keys()获取,常规颜色循环可以使用gca()获取。_get_lines.color_cycle.next()

      【讨论】:

      • 谢谢。我在 iPython 笔记本中尝试了您的解决方案,但只绘制了两点。
      • @Zelong 欢迎您。当我在这里运行代码时,我得到了所有 6 分。三种不同的颜色和两种不同的形状(+ 和 *)。看起来很稀疏,只有 6 个点
      猜你喜欢
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      相关资源
      最近更新 更多