2017-07-21 7 views
2

Ich habe drei Parzellen mit Pandas. In einem der Plots gibt es nicht die Zahl "2", so dass alle Farben der Kategorien sich von denen der anderen unterscheiden. Ist es möglich, eine feste Farbkarte zu haben?Erhalten einer festen Legende colormap

Hier sind die drei Stellplätze:

enter image description here

enter image description here

enter image description here

Und hier ist wie eine der Parzellen aufgebaut ist:

fig, ax = plt.subplots() 

columns = ['Radius', 'FRC', 'Scoring'] 
df = pd.DataFrame(bestof10, columns=columns) 
out = {} 
for column in columns: 
    out[column] = pd.value_counts(df[column]) 

uniq_df = pd.DataFrame(out).fillna(0) 

test = uniq_df.T.plot(kind="bar", stacked=True, ax =ax,rot=0) 

#ax.legend(loc='best'); 
ax.set_ylabel("frequency") 
plt.legend(bbox_to_anchor=(1.2, 1), loc='upper right', ncol=1) 
plt.savefig("WithoutInfluenceOfParameterBestOf10.png",bbox_inches='tight') 

Antwort

2

Eine Möglichkeit, mache ich t wäre in allen nicht-vorhandenen Werten in Datenrahmen füllen (ich Probleme hatte die Legende zu positionieren, wie Sie tun, ist das, warum ich GridSpec verwendet):

import pandas as pd 
from matplotlib import pyplot as plt 
from matplotlib import gridspec 

gs = gridspec.GridSpec(ncols = 5, nrows = 12) 
fig = plt.figure(figsize=(4,8)) 
axes = [ 
    fig.add_subplot(gs[4*i:4*(i+1),:-1]) for i in range(3) 
] 

columns = ['Radius', 'FRC', 'Scoring'] 
dfs = [ 
    pd.DataFrame({ 
     'FRC' :  sum([[i+1]*n for i,n in enumerate([0,0,3,7,0,0,0,])],[]), 
     'Radius' : sum([[i+1]*n for i,n in enumerate([0,0,1,9,0,0,0,])],[]), 
     'Scoring' : sum([[i+1]*n for i,n in enumerate([1,0,1,2,1,2,3,])],[]), 
    }), 
    pd.DataFrame({ 
     'FRC' :  sum([[i+1]*n for i,n in enumerate([0,1,8,11,0,0,0,])],[]), 
     'Radius' : sum([[i+1]*n for i,n in enumerate([0,0,5,15,0,0,0,])],[]), 
     'Scoring' : sum([[i+1]*n for i,n in enumerate([3,2,3,3, 2,3,4,])],[]), 
    }), 
    pd.DataFrame({ 
     'FRC' :  sum([[i+1]*n for i,n in enumerate([0,1,8,11,0,0,0,])],[]), 
     'Radius' : sum([[i+1]*n for i,n in enumerate([0,0,5,15,0,0,0,])],[]), 
     'Scoring' : sum([[i+1]*n for i,n in enumerate([3,2,3,3, 2,3,4,])],[]), 
    }), 
] 
#collect all possible values in a set() by looping through all 
#dataframes 
all_vals=set()  
for df in dfs: 
    for column in columns: 
     all_vals.update(df[column]) 

#looping through the data frames again to produce the plots   
for bestof,ax in zip(dfs, axes):  
    df = pd.DataFrame(bestof, columns=columns) 
    out = {} 
    for column in columns: 
     out[column] = pd.value_counts(df[column]) 

     for val in all_vals: 
      if val not in out[column]: 
       out[column][val] = 0 

    uniq_df = pd.DataFrame(out).fillna(0) 

    test = uniq_df.T.plot(kind="bar", stacked=True, ax=ax, rot=0) 

    ax.legend(loc='best'); 
    ax.set_ylabel("frequency") 
    ax.legend(bbox_to_anchor=(1.2, 1), loc='upper right', ncol=1) 
fig.tight_layout() 
plt.show() 

Dieser Code folgende Abbildung erzeugt: bar plots with correct legends

Hoffe das hilft.