Reputation: 545
I have seen a lot of stuff about this, but I cant seem to get any of it to work, so I thought I'd ask.
I would like to generate plots like this, with a table below the plot showing the count of each category. Can anyone help me out?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
data = {'Category': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c'],
'var1': [1, 2, 2, 4, 5, 3, 4, 5, 4, 7],
'var2': [2, 4, 8, 9, 4, 2, 3, 8, 3, 7]}
df = pd.DataFrame(data)
print(df.head(15))
sns.jointplot(data=df, x='var1', y='var2', hue='Category')
plt.suptitle('Example Data')
plt.show()
Ideally I'd like it to look something like this:
Plot Stuff
_______________________
| Category | a | b | c |
------------------------
| Count | 5 | 3 | 2 |
------------------------
EDIT:
I've updated the script as shown below. I cannot get the table centered, I can only get it to appear on the right side of the plot, and I NEED it to be under the plot, but above would also work.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
data = {'Category': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c'],
'var1': [1, 2, 2, 4, 5, 3, 4, 5, 4, 7],
'var2': [2, 4, 8, 9, 4, 2, 3, 8, 3, 7]}
df = pd.DataFrame(data)
print(df.head(15))
sns.jointplot(data=df, x='var1', y='var2', hue='Category')
plt.suptitle('Example Data')
plt.table(cellText=df.values,
rowLabels=df.index,
colLabels=df.columns,
cellLoc = 'center', rowLoc = 'center',
loc='bottom')
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.show()
Upvotes: 0
Views: 10228
Reputation: 124
You can use bbox
to center your table manually, here is your code with bbox
and adjusted font size:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
data = {'Category': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c'],
'var1': [1, 2, 2, 4, 5, 3, 4, 5, 4, 7],
'var2': [2, 4, 8, 9, 4, 2, 3, 8, 3, 7]}
df = pd.DataFrame(data)
print(df.head(15))
sns.jointplot(data=df, x='var1', y='var2', hue='Category')
plt.suptitle('Example Data')
table = plt.table(cellText=df.values,
rowLabels=df.index,
colLabels=df.columns,
bbox=(-6, -0.65, 6, 0.5))
table.auto_set_font_size(False)
table.set_fontsize(12)
plt.show()
Upvotes: 2
Reputation: 3559
Complimenting @Fabian's answer:
it's possible to place bbox
with respect to the figure, using Transform (fig.transFigure
in this instance):
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
data = {'Category': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c'],
'var1': [1, 2, 2, 4, 5, 3, 4, 5, 4, 7],
'var2': [2, 4, 8, 9, 4, 2, 3, 8, 3, 7]}
df = pd.DataFrame(data)
print(df.head(15))
sns.jointplot(data=df, x='var1', y='var2', hue='Category')
plt.suptitle('Example Data')
plt.subplots_adjust(left=0.2, bottom=0.4)
the_table = plt.table(cellText=df.values,
rowLabels=df.index,
colLabels=df.columns,
cellLoc = 'center', rowLoc = 'center',
transform=plt.gcf().transFigure,
bbox = ([0.3, 0.1, 0.5, 0.2]))
the_table.auto_set_font_size(False)
the_table.set_fontsize(6)
plt.show()
Upvotes: 1