pandas,matplotlib:如何在子图中为相同的列标签分配相同的颜色和线条样式?

3

我有几个数据框,显示了一些具有相似变量(即列名)的对象随时间变化的情况,并且我将它们绘制在子图中。

>>df1.head()


         FR  stim_current  self_excitation    FF_inh  SFA
1  0.000000           0.0         0.000000 -0.075483   -0
2  0.000000           0.0         0.000000 -0.000000   -0
3 -0.000012           0.0         0.000000 -0.001761   -0
4 -0.000033           0.0        -0.000009 -0.003487    0
5 -0.000064           0.0        -0.000027 -0.005178    0

>>df2.head()

      FR    FB_inh  stim_current  self_excitation
1  0.000000 -0.001569             1         0.000000
2  0.017609 -0.000000             1         0.000000
3  0.034867 -0.000200             1         0.010037
4  0.051780 -0.000577             1         0.019874
5  0.068355 -0.001109             1         0.029515

有没有一种方法可以通过列名来指定线条样式,以便例如FR、stim_current和self_excitation在每个子图中具有相同的颜色?比如我想让FR变成蓝色加粗,stim_current是黑色,self_excitation是绿色。我希望不同数据框架之间的差异也能显示在每个子图中的不同颜色上。理想情况下,我还可以重新排列数据框架的列,使得在顶部的图例中显示出在每个数据框架中出现的内容,而变化的内容则会在图例底部显示。
1个回答

3

您可以使用以下方法在不同的子图之间使用一致的颜色和线条风格:

import matplotlib.pyplot as plt
import numpy as np

# load your pandas DataFrames  df1 and df2 here 

ax = [plt.subplot(211), plt.subplot(211)]
pars = {'FR': {'color': 'r'},
        'stim_current': {'color': 'k'}}
ls_style = ['dashed', 'solid']
for ax_idx, name in enumerate(['FR', 'stim_current']):
    for df, ls in zip([df1, df2], ls_style):
        ax[ax_idx].plot(df.index, df[name], ls=ls, **pars[name])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接