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

color_swatch() font size problems with less than 9 colors #32

Open
abubelinha opened this issue Jan 2, 2024 · 6 comments · Fixed by #33
Open

color_swatch() font size problems with less than 9 colors #32

abubelinha opened this issue Jan 2, 2024 · 6 comments · Fixed by #33

Comments

@abubelinha
Copy link

abubelinha commented Jan 2, 2024

Hi. Great and very useful package!

I am testing black/white text thresholds by using color_swatch(), but for some reason it gets nuts when using less than 9 colors.
In those cases, font sizes become huge and they don't fit inside colored boxes.

	import distinctipy, matplotlib.pyplot as plt
	colors = distinctipy.get_colors(8) # with less than 9 colors, color_swatch will use huge font sizes
	thresholds = [0.4,0.6]
	fig, axes = plt.subplots(len(thresholds), 1, figsize=(6, 6))
	for i,t in enumerate(thresholds):
		print(colors,"\n",textcolors)
		distinctipy.color_swatch(colors, show_text=True, ax=axes[i], title="text_threshold={}".format(t), text_threshold=t)
	plt.show()

That code is for 8 colors.
I am using:

  • python v. 3.8.7
  • matplotlib v. 3.6.3
  • distinctipy v. 1.2.3

This is my output for 2, 3, 8 and 9 colors:

2 colors:

color_swatch_2-colors

3 colors:

color_swatch_3-colors

8 colors:

color_swatch_8-colors

9 colors:

color_swatch_9-colors

Is there any way I can choose the font size to be used?
Thanks
@abubelinha

@jack89roberts
Copy link
Collaborator

Hi @abubelinha , I've released a new version (v1.3.1) that should both fix the bug with using it for fewer than 9 colours (hopefully), but also adds a fontsize argument so you can change it manually. Let me know if it works!

@abubelinha
Copy link
Author

abubelinha commented Jan 2, 2024

Thanks a lot.
I tried a normal pip installation upgrade but it didn't change my version.
I guess I must install from repository, correct?
(I am not very used to that so I'll check my notes on how I did it in the past)

@abubelinha
Copy link
Author

abubelinha commented Jan 2, 2024

OK, I could upgrade it to 1.3.1 this way (just for my record):

pip install --upgrade https://github.com/alan-turing-institute/distinctipy/archive/master.zip

It works great now, except that default-size text goes beyond top when selecting just one color (nothing really important):

color_swatch_1-colors

Still haven't tried fontsize argument but I'll do.
Thanks a lot for your quick answer!!

@jack89roberts
Copy link
Collaborator

@abubelinha I think that should be fixed now too if you upgrade again (to 1.3.3), if not I'll re-open the issue as something to look into again in the future because I never really found a good solution for getting the sizing etc. right, it's pretty hacky. Thanks for posting the issue!

@abubelinha
Copy link
Author

abubelinha commented Jan 3, 2024

Thanks a lot. It works OK for my simple tests.

I find issues again with default font size when increasing the number of subplots: i.e. combining color_swatch(1), color_swatch(2) ... color_swatch(n) in the same plot.
That's probably not a good idea and I am not really needed to do that (also, passing fontsize=8 solves the problem).
I just want to let you know.

	import distinctipy, matplotlib.pyplot as plt
	maxcolors = 9
	fig, axes = plt.subplots(maxcolors, 1, figsize=(6, 6))
	for i in range(0,maxcolors):
		colors = distinctipy.get_colors(i+1)
		print(colors,"\n",textcolors)
		distinctipy.color_swatch(colors, show_text=True, ax=axes[i], title="colors={}".format(i+1), 
			text_threshold=0.5
			# , fontsize=8
			)
	plt.show()

maxcolors = 5

color_swatch_1-5-colors

maxcolors = 9

color_swatch_1-9-colors

@jack89roberts jack89roberts reopened this Jan 5, 2024
@jack89roberts
Copy link
Collaborator

Ah ok, I'll leave this open but not something I'll get to fixing immediately, it needs a proper solution for scaling the fontsize to fit within the shapes, which isn't something matplotlib gives out the box as far as I know.

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

Successfully merging a pull request may close this issue.

2 participants