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

时间:2015-07-19 10:23:21

标签: python matplotlib

给出以下DataFrame(在pandas中):

mysql> select idfields from fields where idfields in (132,124,130,125) order by field(idfields,132,124,130,125);
+----------+
| idfields |
+----------+
|      132 |
|      124 |
|      130 |
|      125 |
+----------+

为了生成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

我尝试制作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'] ) X的散点图,由Y着色并由Type整形。我怎么能在matplotlib中实现它?

2 个答案:

答案 0 :(得分: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(与熊猫配合使用)。

答案 1 :(得分:0)

方法是执行以下操作。它不优雅,但有效     将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()