meta4 / mplcal

Create a standard calendar view from python using matplotlib
BSD 2-Clause "Simplified" License
19 stars 9 forks source link

[Feature Request] allow passing colors to events. for better color coding. #4

Open SurajBhari opened 1 year ago

SurajBhari commented 1 year ago

allowing passing colors to days may help in way to interpret holidays or events also allowing a non-colored background may help when you don't want a color.

meta4 commented 1 year ago

This was a relatively simple feature to implement.

Since you can add multiple events to a day, passing a color to the add_event() method doesn't make sense. If the user sets the same day to different colors with different events, it's not clear what is intended.

I added the mplcal method color_day() which takes a day of the month and a Matplotlib color. If this is called on the same day multiple times the last call will set the color.

The easiest way to specify a color is using a string describing one of matplotlib's named colors. That way you don't have to import matplotlib into the script creating the calendar.

Matplotlib-named-colors

This list of colors is from https://stackoverflow.com/questions/22408237/named-colors-in-matplotlib

SurajBhari commented 1 year ago

also the second part of the request ability to change the background color. (currently its white)

SurajBhari commented 1 year ago

I was expecting you to change the text color. but this looks better.

SurajBhari commented 1 year ago

While we are at it. why is this not published on pypi ?

meta4 commented 1 year ago

also the second part of the request ability to change the background color. (currently its white)

Since mplcal is built on top of matplotlib it wouldn't be hard to add a color to the "background" of the calendar. This could be done by calling Patch.set_facecolor() on the figure's patch member f.patch.set_facecolor() somewhere in the _render method. This would color the area outside the calendar squares. The reason I haven't done it yet is I haven't figured out the API. Should we pass background='darkkaki' to the constructor MplCalendar, or the show method, or both? I lean towards show, but could be persuaded towards either of the other two. What is your opinion?

meta4 commented 1 year ago

While we are at it. why is this not published on pypi ?

I'm conflicted.

Pro: It would be cool to have a package on pypi, this would be my first. Pro: It would be easier for others to find it and use it. Con: It was created just to scratch an itch. theWasweisich just found a rather obvious bug in the error handling (which is cool, but humbling). Con: It requires matplotlib to work. For someone who wants to make a calendar, this is not an obvious requirement.

I opened another issue named "Add mplcal to pypi" to move this conversation there. People can "me too" on that issue to give me a sense of who thinks a pypi package would be useful.

Please add a "me too" comment in that issue.

SurajBhari commented 1 year ago

Should we pass background='darkkaki' to the constructor MplCalendar, or the show method, or both?

I personally think to be consistent with the code make a .set_background() func. alongside this you can allow it in the constructor. i don't see point of doing it on show func because that is just a projection method. the attribute should not be changed with projection.

meta4 commented 1 year ago

Regarding the background color, it's already possible with the current version!

The mplcal.show method passes it's arguments to matplotlib.pyplot.subplots, which passes it's arguments to matplotlib.pyplot.figure. So, if you call show with the named argument 'facecolor' set to any one of the colors above it gets passed to mpl.figure which sets the background color to that color.

The calendar below was created with the following code (The only difference from the front-page example, is the "facecolor='chocolate'" argument to show).

from mplcal import MplCalendar

feb = MplCalendar(2017, 2) # 2017, February
feb.add_event(1, '1st day of February')
feb.add_event(5, '         1         2         3         4         5         6')
feb.add_event(5, '123456789012345678901234567890123456789012345678901234567890')
feb.add_event(18, 'OSLL Field Maintenance Day')
feb.add_event(18, 'OSLL Umpire Mechanics Clinic')
feb.add_event(20, 'Presidents day')
feb.add_event(25, 'OSLL Opening Day')
feb.add_event(28, 'T-Ball Angels vs Dirtbags at OSLL')

feb.add_event(14, 'Valentines Day!')
feb.color_day(14, 'mistyrose')

feb.show(facecolor='chocolate')

Again, this works in the current version.

Figure 1