Matplotlib熊猫plot_date颜色类别

时间:2018-07-11 08:41:47

标签: python pandas matplotlib colors seaborn

我有一些数据框:

    Date_Week  Game_Order Game_Mode
0  2010-04-13         124         a
1  2010-06-29           7         a
2  2009-03-17          40         b
3  2010-08-03          54         a
4  2009-09-29          20         e
5  2009-07-07          13         b
6  2009-04-07          29         a
7  2010-03-30          37         b
8  2010-04-13         118         a
9  2010-07-13          12         b
10 2011-08-23          38         c
11 2009-06-02          96         a
12 2010-03-16          48         a
13 2010-08-10          18         b
14 2010-11-09           1         c

其中Date_Week列是日期时间时间戳:

dft['Date_Week'][0]
Out[1043]: Timestamp('2010-04-13 00:00:00')

我正在使用熊猫plot_date()函数进行绘制。

import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib as mpl

fig, ax = plt.subplots(figsize=(22,8))
ax.plot_date(dft['Date_Week'], dft['Game_Order'], marker='o', markersize=2, mew=2)
ax.tick_params('y', colors='k')
ax.set_xlabel('Date')
ax.set_ylabel('Frequency')
ax.set_title('Weekly Games')
ax.tick_params('y', colors='k')
ax.grid(b=True, which='major', color='w', linewidth=1.0)
ax.grid(b=True, which='minor', color='w', linewidth=0.5)
ax.yaxis.grid(True)

xtick_locator = mpl.dates.MonthLocator(interval=6)
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)
ax.xaxis.set_major_locator(xtick_locator)
ax.xaxis.set_major_formatter(xtick_formatter)

xtick_locator = mpl.dates.MonthLocator(bymonth=[2,3,4,5,6,8,9,10,11,12], interval=1)
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)
ax.xaxis.set_minor_locator(xtick_locator)
ax.xaxis.set_minor_formatter(xtick_formatter)


plt.setp(ax.xaxis.get_minorticklabels(), rotation=90, size=10)
plt.setp(ax.xaxis.get_majorticklabels(), rotation=90, size=15)

fig.subplots_adjust(bottom=0.24)
plt.legend()
plt.show()

这给出了:

enter image description here

我想根据Game_Mode列中的类别为点着色。

我做了一些研究,发现了thisthisthisthis

基于此,我尝试了以下每个操作:根据类别将浮点数,seaborn RGB调色板值或特定颜色映射到颜色列:

 # Colouring
categories = dft['Game_Mode'].unique()

# Possible Way 1
colors = np.linspace(0, 1, len(categories))
colordict = dict(zip(categories, colors))
# Possible Way 2
colors = sns.color_palette()
colordict_1 = dict(zip(categories, colors))
# Possible Way 3
colors = ['r', 'g', 'b', 'c' ,'k']
colordict_2 = dict(zip(categories, colors))

dft["Colour"] = dft['Game_Mode'].apply(lambda x: colordict[x])
dft["Colour_1"] = dft['Game_Mode'].apply(lambda x: colordict_1[x])
dft["Colour_2"] = dft['Game_Mode'].apply(lambda x: colordict_2[x])

dft
    Date_Week  Game_Order Game_Mode    Colour  \
0  2010-04-13         124         a  0.000000   
1  2010-06-29           7         a  0.000000   
2  2009-03-17          40         b  0.333333   
3  2010-08-03          54         a  0.000000   
4  2009-09-29          20         e  0.666667   
5  2009-07-07          13         b  0.333333   
6  2009-04-07          29         a  0.000000   
7  2010-03-30          37         b  0.333333   
8  2010-04-13         118         a  0.000000   
9  2010-07-13          12         b  0.333333   
10 2011-08-23          38         c  1.000000   
11 2009-06-02          96         a  0.000000   
12 2010-03-16          48         a  0.000000   
13 2010-08-10          18         b  0.333333   
14 2010-11-09           1         c  1.000000   

                                            Colour_1 Colour_2  
0   (0.298039215686, 0.447058823529, 0.690196078431)        r  
1   (0.298039215686, 0.447058823529, 0.690196078431)        r  
2   (0.333333333333, 0.658823529412, 0.407843137255)        g  
3   (0.298039215686, 0.447058823529, 0.690196078431)        r  
4    (0.76862745098, 0.305882352941, 0.321568627451)        b  
5   (0.333333333333, 0.658823529412, 0.407843137255)        g  
6   (0.298039215686, 0.447058823529, 0.690196078431)        r  
7   (0.333333333333, 0.658823529412, 0.407843137255)        g  
8   (0.298039215686, 0.447058823529, 0.690196078431)        r  
9   (0.333333333333, 0.658823529412, 0.407843137255)        g  
10  (0.505882352941, 0.447058823529, 0.698039215686)        c  
11  (0.298039215686, 0.447058823529, 0.690196078431)        r  
12  (0.298039215686, 0.447058823529, 0.690196078431)        r  
13  (0.333333333333, 0.658823529412, 0.407843137255)        g  
14  (0.505882352941, 0.447058823529, 0.698039215686)        c 

但是,当我尝试使用任何Colour列进行绘图时,出现以下错误:

0:

# Colour error
fig, ax = plt.subplots(figsize=(22,8))
ax.plot_date(dft['Date_Week'], dft['Game_Order'], marker='o', markersize=2, mew=2, c=dft['Colour'])

Out[1058]: [<matplotlib.lines.Line2D at 0x289091d0>]Traceback (most recent call last):

  File "path\Anaconda3\lib\site-packages\IPython\core\formatters.py", line 307, in __call__
    return printer(obj)

  File "C:\Users\p\Anaconda3\lib\site-packages\IPython\core\pylabtools.py", line 240, in <lambda>
    png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))

  File "C:\Users\p\Anaconda3\lib\site-packages\IPython\core\pylabtools.py", line 124, in print_figure
    fig.canvas.print_figure(bytes_io, **kw)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 2200, in print_figure
    **kwargs)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 545, in print_png
    FigureCanvasAgg.draw(self)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 464, in draw
    self.figure.draw(self.renderer)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1144, in draw
    renderer, self, dsu, self.suppressComposite)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 2426, in draw
    mimage._draw_list_compositing_images(renderer, self, dsu)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\lines.py", line 828, in draw
    rgbaFace = self._get_rgba_face()

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\lines.py", line 1340, in _get_rgba_face
    rgbaFace = mcolors.to_rgba(facecolor, self._alpha)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\colors.py", line 143, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)

  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\colors.py", line 198, in _to_rgba_no_colorcycle
    raise ValueError("RGBA sequence should have length 3 or 4")

ValueError: RGBA sequence should have length 3 or 4

<matplotlib.figure.Figure at 0x2c4705c0>

1

# Colour_1 error:
...
  File "C:\Users\p\Anaconda3\lib\site-packages\matplotlib\colors.py", line 194, in _to_rgba_no_colorcycle
    raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))

ValueError: Invalid RGBA argument: 0     (0.298039215686, 0.447058823529, 0.690196078431)

2

# Colour 2 error:

...
  File "C:\Users\p\Anaconda3\lib\site-packages\pandas\core\generic.py", line 2970, in __getattr__
    return object.__getattribute__(self, name)

AttributeError: 'Series' object has no attribute 'lower'

<matplotlib.figure.Figure at 0x29ec9dd8>

我已经搜索了这些错误,并提出了以下问题:

https://github.com/matplotlib/matplotlib/issues/7603 https://github.com/matplotlib/matplotlib/issues/6266/ https://github.com/matplotlib/matplotlib/issues/2148 Setting colors using color cycle on date plots using `plot_date()`

这些表明这可能与plot_date有关,但是建议的选项均无效。

有人可以告诉我如何解决吗?

1 个答案:

答案 0 :(得分:1)

为了李艾莎的利益:

根据@ImportanceOfBeingErnest的回答,我必须针对每种游戏类型和游戏顺序进行单独绘制。

我不得不扩展创建游戏顺序列的方式,而不是进行global order计算每种游戏子类型的顺序。这意味着必须创建新列"Game_Order_a" "Game_order_b"等,这些列仅具有对应于该game_mode的行的值,并且在其他任何地方都具有NaN(即仅具有特定的游戏类型)。

然后,这意味着分别绘制数据的子集并 手动设置Seaborn分配颜色 。这些颜色由图例处理程序拾取:

因此,对于5个不同的Game_Mode,我这样做了:

fig, ax = plt.subplots()
# Subset your data by choosing where Game_Mode = 'a'
ax.plot_date(df[df['Game_Mode'] == 'a']['Date_Week_a'], df[df['Game_Mode'] == 'a']['Game_Order_a'], marker='o', markersize=2, mew=2, label='a')
ax.plot_date(df[df['Game_Mode'] == 'b']['Date_Week_b'], df[df['Game_Mode'] == 'b']['Game_Order_b'], marker='o', markersize=2, mew=2, label='b')
# ... etc for each type that you have
plt.legend(fontsize=15, markerscale=4)
plt.show()

这会为您提供每种类型的图,并自动选择颜色。如果您愿意使用c=参数,可以指定它们,但对我而言不是必需的。