mlxtend icon indicating copy to clipboard operation
mlxtend copied to clipboard

Possible to change the figure size in category_scatterplot?

Open hellojinwoo opened this issue 4 years ago • 1 comments

I am studying machine learning, and visualization has been always thorn in my flesh. I thank this library 'mlxtend' to let me easily draw categorical scatter plots out of pd.DataFrame. However, I have 2 questions regarding it.

  • I have thoroughly read your examples from this url: http://rasbt.github.io/mlxtend/user_guide/plotting/category_scatter/#overview

Q1. Is it possible to change the figure size of the plot?

from mlxtend.plotting import category_scatter does not accept figsize as a parameter. How can I change the figure size of the plot?

Q2. How can I annotate the each point in scatter plots?

It would be very nice if I can annotate each points in a scatter plot by their label names. Can I do this with mlxtend?

hellojinwoo avatar Nov 18 '19 00:11 hellojinwoo

Q1. Is it possible to change the figure size of the plot?

Yes, that's definitely possible. I think this is among the oldest functions in mlxtend, and the API is not that obvious, but here is a working example (it should probably be added to the documentation, so I am leaving this issue open as a reminder).

Here are some mandatory imports and some data:

import numpy as np
from io import BytesIO
import matplotlib.pyplot as plt
from mlxtend.plotting import category_scatter


csvfile = """1,10.0,8.04
1,10.5,7.30
2,8.3,5.5
2,8.1,5.9
3,3.5,3.5
3,3.8,5.1"""

ary = np.genfromtxt(BytesIO(csvfile.encode()), delimiter=',')

Then, you can resize using set_size_inches, e.g.,

fig = category_scatter(x=1, y=2, label_col=0, 
                       data=ary, legend_loc='upper left')

fig.set_size_inches(3, 4)

Q2. How can I annotate the each point in scatter plots?

This could be done uses the "annotate" method. For example, using the dataset above, you could do it as follows:


fig = category_scatter(x=1, y=2, label_col=0, 
                       data=ary, legend_loc='upper left')


x = ary[:, 1]
y = ary[:, 2]
l = ['a', 'b', 'c', 'd', 'e', 'f']

for i, txt in enumerate(l):
    fig.axes[0].annotate(txt, (x[i], y[i]))

plt.show()

Unknown

rasbt avatar Nov 18 '19 18:11 rasbt