首页 文章

如何使用matplotlib绘制不同颜色和形状的多个组?

提问于
浏览
0

给定以下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中实现它?

2 回答

  • 2

    随着更多熊猫:

    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()
    

    enter image description here

    但是,对于这种多维情节,请查看seaborn(与熊猫配合使用) .

  • 0

    一种方法是执行以下操作 . 它不优雅,但作为plt导入matplotlib导入matplotlib.pyplot作为mpl import numpy as 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()

相关问题