gpt4 book ai didi

python - pandas,matplotlib : a way to assign same colors, 子图中相同列标签的线条样式?

转载 作者:行者123 更新时间:2023-11-28 16:33:56 26 4
gpt4 key购买 nike

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

>>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 为绿色。我还希望数据帧之间的任何不同之处都以不同的颜色显示在每个子图中。理想情况下,我还可以重新排序数据框的列,以便每个数据框中显示的内容都在顶部的图例中,而变化的内容将出现在图例的底部。

最佳答案

可以通过以下方法在不同的子图之间使用一致的颜色和线条样式,

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

关于python - pandas,matplotlib : a way to assign same colors, 子图中相同列标签的线条样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28989156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com