Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legend title size does not get updated #14

Open
jankaWIS opened this issue Sep 15, 2021 · 4 comments
Open

Legend title size does not get updated #14

jankaWIS opened this issue Sep 15, 2021 · 4 comments

Comments

@jankaWIS
Copy link

jankaWIS commented Sep 15, 2021

Maybe it's not a CanD issue but I have not figured out why it's doing so and how to fix it. If I have a plot and want to set a fontsize, I do:

c.set_font("Lucida", size=18)

That works for all parts of the plot (which is also a bit not good -- how could I have two sizes, one for titles and one for ticks?) except for legend title. I have no idea why. Passing the size directly to matplotlib works (commented line). Here is my code and example:

from cand import Canvas, Vector, Point
import matplotlib.pyplot as plt

palette2 = ["navy", "blue", "dodgerblue","slategrey"]

# CanD
a = 6
c = Canvas(a*4+4, 20, "cm")

# set font
c.set_font("Lucida", size=18)

c.add_grid(["cond1"], 1, Point(2, 2, "cm"), Point(a*4, 18.1, "cm"), spacing=Vector(.5, 1, "cm"), unitname="grid")

# extract data
ax = c.axes["cond1"]

for i, col in enumerate(palette2):
    x, y = None, None
    y = np.arange(3)*i
    x = np.array([1,2,3])

    # plot
    ax.scatter(x,y,color=col, label=f"{col}")
    ax.set_xticks(x)

    ax.set_ylabel("y-ax")
    ax.set_xlabel("x-ax")
#    ax.legend(title="Colour", fontsize=8, title_fontsize=14)  
    ax.legend(title="Colour")  

c.show()    

image

@mwshinn
Copy link
Owner

mwshinn commented Sep 15, 2021

Thanks for the bug report. Yes, this is a CanD issue, and it does need to be fixed. Right now, CanD doesn't have great support for matplotlib legends. For right now, as a workaround, you might be able to construct a legend using c.add_legend() instead of ax.legend(). Alternatively, as a workaround, you could remove the title on the legend, and add a title for it using c.add_text().

@mwshinn
Copy link
Owner

mwshinn commented Sep 15, 2021

(Also, as a side note, it might be easier to use add_axis instead of add_grid here, since you only have one axis.)

@jankaWIS
Copy link
Author

Thanks, glad it's not just me :D. I think that for a workaround, this works:
ax.legend(title="Colour", fontsize=8, title_fontsize=14)

And about your proposed solution, that would only work if only one legend is present in the entire canvas, no? Like having two plots there each with a legend would break it, one couldn't use c.add_legend() anymore, am I right?

Could you please elaborate more on how to use the font and fontsize in CanD in general? Like if one would want to have first level titles and suptitles and ticks... I guess one could have a workaround with c.add_text() but that's clumsy and still doesn't entirely solve the issue.

(as a side note -- it was part of a bigger figure and I just crop this one out and was lazy to make it pretty, sorry, and thanks for pointing it our :))

@mwshinn
Copy link
Owner

mwshinn commented Sep 16, 2021

You can use c.add_legend to add as many legends as you want!

The best way to add titles/suptitles/etc in CanD is via add_text(). Matplotlib has some defaults built in with default positions and default formatting to make thing simpler. CanD lets you have as many titles as you want with whatever sizes/positioning you want. You can set the font size for ticks and titles with c.set_font() using the ticksize and titlesize arguments.

Instead of c.ax([axis name]).set_title("Title"), just call c.add_text("Title", Point(.5, 1.1, "axis_[axis name]"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants