简体   繁体   English

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

[英]How to plot multiple groups in different colors and shapes with matplotlib?

Given the following DataFrame (in pandas): 给定以下DataFrame(以熊猫为单位):

        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

In order to generate the DataFrame: 为了生成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']
       )

I tried to make a scatter plot of X and Y , colored by Type and shaped by Region . 我试图绘制XY的散点图,分别按Type着色和按Region整形。 How could I achieve it in matplotlib? 如何在matplotlib中实现它?

With more Pandas: 更多熊猫:

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

在此处输入图片说明

For this kind of multi-dimensional plot, though, check out seaborn (works well with pandas). 但是,对于这种多维情节,请查看seaborn(适用于熊猫)。

An approach would be to do the following. 一种方法是执行以下操作。 It is not elegant, but works import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np plt.ion() 它不是很优雅,但是可以将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)

I am not familiar with Panda, but there must some more elegant way to do the filtering. 我对Panda不熟悉,但是必须有一些更优雅的方法来进行过滤。 A marker list can be obtained using markers.MarkerStyle.markers.keys() from matplotlib and the conventional color cycle can be obtained using gca()._get_lines.color_cycle.next() 可以使用matplotlib中的markers.MarkerStyle.markers.keys()获得标记列表,而常规颜色周期可以使用gca()._ get_lines.color_cycle.next()获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM