2016-08-04 3 views
3

광범위한 플로팅 스크립트의 경우 matplotlibs rcParams를 사용하여 팬더 DataFrames의 표준 플롯 설정을 구성합니다.팬더 데이터 프레임 플롯 : 기본 색상 맵 영구 변경

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