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

Extend color legend size for scatter charts for matplotlib #286

Merged
merged 24 commits into from
Mar 16, 2021

Conversation

caitlynachen
Copy link
Contributor

@caitlynachen caitlynachen commented Feb 24, 2021

Changed figsize for scatter charts
Screen Shot 2021-02-23 at 5 53 15 PM
Screen Shot 2021-03-07 at 9 04 17 PM

@dorisjlee
Copy link
Member

Thanks @caitlynachen! This looks like a good start. Here are a couple of cases where the figure size doesn't quite work out. The plot figure looks overly stretched out. We might also need to abbreviate the legend when the number of colors is too large.
image
image

@caitlynachen
Copy link
Contributor Author

fixed corner cases @dorisjlee mentioned

Screen Shot 2021-02-24 at 10 40 11 AM
Screen Shot 2021-02-24 at 10 39 52 AM

@dorisjlee
Copy link
Member

dorisjlee commented Feb 25, 2021

Thanks @caitlynachen! A few more comments on the latest changes:

  • For the abbreviated chart, could we do "..." to indicate that there is more color in the legend than we are displaying? I see the code for unique[i] = unique[i][:26] + "..." but this is not displayed on the chart. In the Brand legend example, there are only 16 colors displayed in the legend but there is 29 different Brand in the dataset.

  • When we change the width and height of the resulting plot, the fontsize becomes much smaller. Is there a way we can ensure that the font and graphics are consistent across the charts (regardless of size of chart)?
    image

for i in range(len(unique)):
unique[i] = str(unique[i])
if len(unique[i]) > 26:
unique[i] = unique[i][:26] + "..."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not showing up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean by this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the abbreviation "..." is not showing up in the legend, see comment above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o hmm i think it was working on my end
109049161-c787d900-768c-11eb-95d0-44363d65fabc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you seeing something differently

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm able to see it.

Copy link
Member

@dorisjlee dorisjlee Mar 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Oops sorry, I meant when the ... vertically (not horizontally) when there are too many color categories.

In the Brand legend example, there are only 16 colors displayed in the legend but there is 29 different Brand in the dataset.

In this example, you would have ... under the amc legend

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is a good point, a user might be confused why a specific category is not showing up, so something in the legend would be nice to address this. Another idea rather than using the '...' (which seems a bit off to me) could be to use multiple columns. Try maybe something like this: https://stackoverflow.com/questions/58344791/how-to-handle-categorical-legends-with-too-many-entries-to-match-a-fixed-chart-h

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh the "..." is not for the length of the legend, but rather the individual categories. I checked and Altair also didn't have a "..." at the end of the list, so didn't include it in this PR. If you want, maybe I can tackle that issue separately

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an issue #295 for this new change

for i in range(len(unique)):
unique[i] = str(unique[i])
if len(unique[i]) > 26:
unique[i] = unique[i][:26] + "..."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm able to see it.

@dorisjlee
Copy link
Member

Hi @caitlynachen, Thanks for adding in #295. Could you look into addressing the earlier comment on consistency across fontsizes when the image stretching happens?

  • When we change the width and height of the resulting plot, the fontsize becomes much smaller. Is there a way we can ensure that the font and graphics are consistent across the charts (regardless of size of chart)?
    image

@caitlynachen
Copy link
Contributor Author

Added fontsize to xlabel, ylabel and legend attributes to maintain fontsize to be as large as possible within the range of plt.tight_layout

@dorisjlee dorisjlee changed the title Extend color legend size for scatter charts Extend color legend size for scatter charts for matplotlib Mar 8, 2021
@caitlynachen
Copy link
Contributor Author

caitlynachen commented Mar 8, 2021

In response to @dorisjlee 's comment about reduced font size on these scatter charts with color legends, I tried a number of solutions:

  1. Attempting to increase the figure size (height and width) - plt.subplots(figsize=(20, 6))
  2. Setting rcParams after creating larger figure size to larger font size
  3. Removing plt.tight_layout() to remove automatic sizing
  4. Setting plt.gcf().subplots_adjust(bottom=0.15) and plt.gcf().subplots_adjust(left=0.15) to add additional padding space for labels
  5. Setting plt.rcParams['figure.autolayout'] = True to fit charts to figure size

Despite the changes, the font size still remained smaller than normal.

Screen Shot 2021-03-08 at 9 49 05 AM

Not entirely sure why this is the case, especially since when I print out plt.rcParams['axes.labelsize']right before plt.savefig, it says the font is as expected at 16, but doesn't display as such. Let me know if anyone else is familiar with Matplotlib frontend and would like to help out on this issue!

Resources:
https://stackoverflow.com/questions/6774086/why-is-my-xlabel-cut-off-in-my-matplotlib-plot
https://matplotlib.org/3.1.0/api/matplotlib_configuration_api.html

@dorisjlee dorisjlee merged commit 182a4b7 into lux-org:master Mar 16, 2021
@dorisjlee
Copy link
Member

Thanks for the explanation @caitlynachen! I'm merging this in for now, for the matplotlib font size issue, could you create a separate issue outlining this problem? I believe this is a more general issue with the font size and figures for matplotlib and is not particular to just the axis label (affects all text and elements on the figure when the figure is resized).

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 this pull request may close these issues.

None yet

3 participants