plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
15.73k stars 2.51k forks source link

How to preset to use specific typeface file #3229

Open finlabhk opened 3 years ago

finlabhk commented 3 years ago

I have deployed a chart generation service in Heroku. But the chart cannot show the correct text because Heroku is missing specific typeface file. And I cannot copy the required typeface file to "/usr/share/fonts/". How to make the plotly to use my custom typeface file to generate charts? Thank you very much.

nicolaskruchten commented 3 years ago

Are you using Kaleido for this? If you can tell us more about how you've set up your service we'd be able to help you better :)

finlabhk commented 3 years ago

Are you using Kaleido for this? If you can tell us more about how you've set up your service we'd be able to help you better :)

Yes, I am using Plotly and kaleido to generate charts. I have faced the same issue in google colab which is because of missing required typeface file. Unlike the matplotlib, which I can use Fontproperties() to specify the path of typeface file. Therefore, I would copy the typeface file to /usr/share/fonts/truetype/ before I generate charts. It works in google colab environment.

But I cannot apply the same method in Heroku environment because /usr/share/fonts/truetype/ is read only. So I wanna to know do Plotly can specify the typeface file location when generating charts? Just like Fontproperties() in matplotlib?

Thank you for your reply.

iwensu0313 commented 2 years ago

I have a similar question. I am working in JupyterHub and was able to add the .tff file for Franklin Gothic into /usr/share/fonts/truetype with sudo to override the initial permissions issue.

I am able to render a matplotlib plot with the downloaded font. I had to first clear the matplotlib cache rm -fr ~/.cache/matplotlib before matplotlib was able to pick up the newly installed font. Then this works

import matplotlib.pyplot as plt
font = 'Franklin Gothic'

plt.figure(figsize=(10, 6))
plt.scatter(x=[1, 2, 3, 4], y=[1, 2, 3, 4], s=500, color="darkcyan")
plt.text(2.5, 2, "Annotation"
         , fontname=font, fontsize=24
         )
plt.xlabel(
    "Horizontal Label", 
    fontname=font, fontsize=20
)
plt.ylabel("Vertical label", fontname=font, fontsize=20)
plt.title(font, fontsize=33,fontname=font, fontproperties=font_prop)

however, this does not work:

import plotly.graph_objs as go

data = [
    go.Scatter(
        x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
        y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
    )
]
font = 'Franklin Gothic'
layout = go.Layout(width=500, height=300,
    title=font,
    font=dict(family=font, size=18)
)
fig = go.Figure(data=data, layout=layout)
fig

I replicated empet's code here except using go.Figure() instead of go.FigureWidget(). Not sure if that makes a difference, but I was having trouble getting the latter to work, so I just stuck with how I usually render plotly graphs..

Is there a different way to specify the font via a url, like you can do with html/javascript here?