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

Feature Request: Watermark on Plots #164

Open
PowerrNuller opened this issue Jun 10, 2020 · 4 comments
Open

Feature Request: Watermark on Plots #164

PowerrNuller opened this issue Jun 10, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@PowerrNuller
Copy link

Is your feature request related to a problem? Please describe.
Ability to add Watermarks to charts with alpha setting.

Describe the solution you'd like
Function or argument to add a logo/watermark to charts that takes a ,png image file.

Describe alternatives you've considered
None

Additional context
None

@PowerrNuller PowerrNuller added the enhancement New feature or request label Jun 10, 2020
@AminSaqi
Copy link

Any workaround or trick for that?

@AminSaqi
Copy link

@DanielGoldfarb I can work on it in my free times. Guide me to do it.

@DanielGoldfarb
Copy link
Collaborator

@AminSaqi
Amin,
Thank you. It appears from matplotlib documentation that a watermark is just a mater of displaying either text, or an image, on the plot, with an Alpha less than one (typically 0.5).

  1. https://matplotlib.org/3.2.1/gallery/images_contours_and_fields/watermark_image.html
  2. https://matplotlib.org/3.2.1/gallery/text_labels_and_annotations/watermark_text.html
  3. https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.imshow.html

I've included the 3rd reference above because it is Axes based instead of Figure based, and also has an alpha parameter.


An easy workaround (instead of implementing this inside mplfinance) would be to set returnfig=True:

fig, ax = mpf.plot(df,...,returnfig=True,...)

The list of axes returned correspond, in order, to the panels, returning two axes (primary followed by secondary) for each panel. Thus ax[0] is the primary axes for panel 0, ax[1] is the secondary axes for panel 0, ax[2] is the primary for panel 1, etc. After getting the Figure and Axes list returned from mpf.plot() you can set you watermark on either the Figure or Axes, and then follow up with a call to matplotlib.pyplot.show() or similar.


If you want to implement this, I have some ideas, but I have to run right now ... will be back in a few hours and jot down my ideas (that include making these more general) and gladly provide guidance. In the meantime, you can play with the above workaround, and gain some experience and familiarity with the matplotlib methods needed to implement this. Thank you very much for getting involved. Much appreciated. All the best. --Daniel

@teddyrowan
Copy link
Contributor

teddyrowan commented Aug 19, 2020

edit: I didn't realize that the request for for a png overlay. here's a way to do it with simple text though.

I thought I would just drop an example since no one had and I imagine that this is a somewhat common want from users.

Just a standard plot with your custom style and returnfig = True

fig, axes = mpf.plot(df, **kwargs, style=s, 
        title="\n\n" + data_title,
        datetime_format=' %m/%d',
        ylabel="Option Price ($)",
        returnfig=True)                

Now we create a textbox with no edges or facecolor and add whatever watermarking text you want.

waterstr = 'Watermark'
props = dict(boxstyle='square', 
             facecolor='none', 
             alpha=0, 
             edgecolor='none')
labelfont = {'fontname':'DejaVu Sans', 'fontsize':40, 'color':'r', 'alpha':0.20}
axes[0].text(0.20, 0.65, waterstr, 
            transform=axes[0].transAxes, 
            bbox=props, 
            **labelfont)
fig.savefig('watermark_demo.png')    

And the result:
watermark_demo

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

No branches or pull requests

4 participants