2016-08-04 74 views
3

对于大量绘图脚本,我使用matplotlibs rcParams为熊猫数据框配置一些标准绘图设置。熊猫数据框图:永久更改默认颜色映射

这非常适用于颜色和字体大小,但没有为默认颜色映射描述here

这是我目前的做法:

# -*- coding: utf-8 -*- 
import pandas as pd 
import numpy as np 
import matplotlib 
import matplotlib.pyplot as plt 
from matplotlib import cm 


# global plotting options 
plt.rcParams.update(plt.rcParamsDefault) 
matplotlib.style.use('ggplot') 
plt.rcParams['lines.linewidth'] = 2.5 
plt.rcParams['axes.facecolor'] = 'silver' 
plt.rcParams['xtick.color'] = 'k' 
plt.rcParams['ytick.color'] = 'k' 
plt.rcParams['text.color'] = 'k' 
plt.rcParams['axes.labelcolor'] = 'k' 
plt.rcParams.update({'font.size': 10}) 
plt.rcParams['image.cmap'] = 'Blues' # this doesn't show any effect 


# dataframe with random data 
df = pd.DataFrame(np.random.rand(10, 3)) 

# this shows the standard colormap 
df.plot(kind='bar') 
plt.show() 

# this shows the right colormap 
df.plot(kind='bar', cmap=cm.get_cmap('Blues')) 
plt.show() 

第一条曲线不通过颜色表使用颜色表(它通常应做): enter image description here

它只能如果我把它作为一个参数,如第二个图:

enter image description here

是否有任何方法可以永久定义熊猫DataFrame图的标准颜色映射?

在此先感谢!

回答

1

有没有支持,官方的方式来做到这一点;你被卡住,因为熊猫的内部_get_standard_colors function是在硬编码使用matplotlib.rcParams['axes.color_cycle']回退到list('bgrcmyk')

colors = list(plt.rcParams.get('axes.color_cycle', 
           list('bgrcmyk'))) 

然而有可以使用,各种黑客攻击;最简单的一个,它适用于所有pandas.DataFrame.plot()通话,是包装pandas.tools.plotting.plot_frame

import matplotlib 
import pandas as pd 
import pandas.tools.plotting as pdplot 

def plot_with_matplotlib_cmap(*args, **kwargs): 
    kwargs.setdefault("colormap", matplotlib.rcParams.get("image.cmap", "Blues")) 
    return pdplot.plot_frame_orig(*args, **kwargs) 

pdplot.plot_frame_orig = pdplot.plot_frame 
pdplot.plot_frame = plot_with_matplotlib_cmap 
pd.DataFrame.plot = pdplot.plot_frame 

在笔记本上进行测试:

%matplotlib inline 
import pandas as pd, numpy as np 
df = pd.DataFrame(np.random.random((1000,10))).plot() 

...产量:

blue_plot