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:
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):
Then there's a second graph, its dataFrame, vis
, looks like this:
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:
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?