TYMING
TYMING

Reputation: 1

Seaborn Plot showing only legend header and Annotation not in place for only 1 plot in FacetGrid Plot

First time using seaborn plots and managed to come out with the plot i want however there is issue as annotation in only 1 plot(row1,column8)is not in place and legends only show the variable but not the line/color example.

Do assist if you know of the solution or any issues with the code

Dataframe

       State  Count  Month  Year
  0       AK   2988      1  2005
  1       AK   2734      2  2005
  2       AK   3055      3  2005
  3       AK   2981      4  2005
  4       AK   3434      5  2005
         ...    ...    ...   ...
  1226    WY    849      8  2006
  1227    WY    723      9  2006
  1228    WY    600     10  2006
  1229    WY    572     11  2006
  1230    WY    585     12  2006

Code

q3plot = sns.FacetGrid(q3, col="State",col_wrap=(8),hue="Year",sharey=False, sharex=True, legend_out=True)
def f(x,y, **kwargs):
    ax = sns.pointplot(x,y,**kwargs)
    for i in range(len(x)):
        ax.annotate(str(y.values[i]), xy=(x.values[i]-1, y.values[i]),fontsize=6,
                    xytext = (0,10), textcoords="offset points",
                color=kwargs.get("color","k"), 
                bbox=dict(pad=.9,alpha=0.2, fc='none',color='none'),
                va='center', ha='center',weight='bold')
q3plot.map(f, "Month","Count").add_legend()

Current Plot

edit:

    x = data[x_col]
    y = data[y_col]
    z = q4c["AirportOrigin"]
    for i in range(len(x)):
        plt.annotate(str(z.values[i]), xy=(x.values[i]-1, y.values[i]), fontsize=3,
                     xytext=(0, 10), textcoords="offset points",
                     color=kwargs.get("text_color", "k"),
                     va='center', ha='center', weight='bold')

g4 = sns.catplot(kind='point', data=q4c, x="id", y="TotalDelay", hue="TailNum", palette='spring',
                col="TailNum", col_wrap=4, height=5, aspect=1.7, sharey=False, sharex=False, legend_out=True)
g4.map_dataframe(do_annotate2, 'id', 'TotalDelay', text_color='navy')
plt.show()```

Upvotes: 0

Views: 474

Answers (1)

JohanC
JohanC

Reputation: 80329

Calling sns.FacetGrid() directly usually isn't needed. Things work easier when the more custom sns.catplot() is used. (The missing annotations for DC seem to be related to a missing year.)

Here is how the code would look like using sns.catplot().

from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

def do_annotate(x_col, y_col, data, color, **kwargs):
    x = data[x_col]
    y = data[y_col]
    for i in range(len(x)):
        plt.annotate(str(y.values[i]), xy=(x.values[i]-1, y.values[i]), fontsize=6,
                     xytext=(0, 10), textcoords="offset points",
                     color=kwargs.get("text_color", "k"),
                     va='center', ha='center', weight='bold')

states = ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS',
          'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV',
          'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY']
# first create some test data somewhat similar to the givens
q3 = pd.DataFrame({'State': np.repeat(states, 12 * 2),
                   'Count': np.random.randint(100, 5000, len(states) * 12 * 2),
                   'Month': np.tile(range(1, 13), len(states) * 2),
                   'Year': np.tile(np.repeat([2005, 2006], 12), len(states))})
q3 = q3[~((q3['State'] == 'DC') & (q3['Year'] == 2005))] # remove one year for DC

g = sns.catplot(kind='point', data=q3, x="Month", y="Count", hue="Year", palette='spring',
                col="State", col_wrap=8, height=2.6, aspect=1.7, sharey=False, sharex=True, legend_out=True)
g.map_dataframe(do_annotate, 'Month', 'Count', text_color='navy')
plt.show()

sns.catplot with kind='point'

Upvotes: 1

Related Questions