0

I have two functions which convert Pandas dataFrames into graphs using Seaborn. First one works, the second one does not, and they are virtually the same.

Here is the first one:

This is how my first dataFrame, vis, looks like:enter image description here

Here it is by using vis.head(16).to_clipboard(sep=',', index=True)

,region,Viditeľnosť,Počet
0,JHC,v noci - nezhoršená,4863
1,PHA,v noci - nezhoršená,20459
2,PLK,v noci - nezhoršená,5144
3,STC,v noci - nezhoršená,22586
4,JHC,v noci - zhoršená,683
5,PHA,v noci - zhoršená,1102
6,PLK,v noci - zhoršená,607
7,STC,v noci - zhoršená,2226
8,JHC,ve dne - nezhoršená,18452
9,PHA,ve dne - nezhoršená,95753
10,PLK,ve dne - nezhoršená,14899
11,STC,ve dne - nezhoršená,55308
12,JHC,ve dne - zhoršená,1989
13,PHA,ve dne - zhoršená,7254
14,PLK,ve dne - zhoršená,1701
15,STC,ve dne - zhoršená,8196

And here is the code to render its graph:

ax = sns.catplot(x='region', hue='region', y='Počet',col='Viditeľnosť', data=vis, kind='bar', palette='hls',col_wrap=2,sharex=False, dodge=False)

for axis in ax.axes:
    axis.bar_label(axis.containers[0])
    axis.bar_label(axis.containers[1])
    axis.bar_label(axis.containers[2])
    axis.bar_label(axis.containers[3])
    axis.set_xlabel('Kraj')

ax.fig.tight_layout()
ax.add_legend()

The resulting graph looks like this (and if I don't use add_legend, no legend is added to the graph):

enter image description here

Then there's a second graph, its dataFrame, vis, looks like this: enter image description here

The code to render a graph from it is this:

ax = sns.catplot(x='month', hue='Druh srážky', y='Počet',col='region', data=dir, kind='bar', palette='hls',col_wrap=2,sharex=False)
ax.set_titles(col_template="Kraj: {col_name}")
ax.fig.tight_layout()
ax.add_legend()

The resulting graph looks like this - it has its legend doubled for no apparent reason:

enter image description here

Why is this happening? The two graphs are both a catplot, yet the first one does not add a legend implicitly, while the second does. How do I fix this?

ampersander
  • 208
  • 2
  • 9
  • 1
    In the first case, adding a legend is redundant, as each color is already identified by the x-axis label. In terms of data visualization best practice, you should not put a legend for the first case. – Trenton McKinney Dec 08 '22 at 16:49
  • 1
    In the second case, do not use `ax.add_legend()`. `hue` should automatically add a legend. It is appropriate to add a legend in the second case, because the colors are not identified by the x-axis tick labels. – Trenton McKinney Dec 08 '22 at 16:50
  • @TrentonMcKinney Thank you for your answer, I apologize for not including the dataframe in its correct format. Editted my answer. If hue adds a legend automatically, why is it not added in the first graph without using `show_legend`? The hue is specified in the first graph but no legend is drawn unless I specifically write a command for that. – ampersander Dec 08 '22 at 16:55
  • 1
    The first comment already states why. – Trenton McKinney Dec 08 '22 at 16:56
  • 1
    For `bar_label`, given `g = sns.catplot(...)`, it should be `for ax in g.axes.ravel(): for c in ax.containers: ax.bar_label(c, label_type='edge')`. As shown in this [answer](https://stackoverflow.com/a/68851142/7758804). – Trenton McKinney Dec 08 '22 at 16:58
  • 1
    @TrentonMcKinney Thank you for your time and answers, it all makes sense now. – ampersander Dec 08 '22 at 17:03

0 Answers0