When working with plotly in Julia, you may come across the need to customize the fonts used in your plots. By default, plotly uses a set of predefined fonts, but you may want to use different fonts to match your desired style or branding. In this article, we will explore three different ways to solve the question of available fonts for plotting inside plotlyjs in Julia.
Option 1: Using the `font` attribute
One way to specify the font for your plotly plots is by using the `font` attribute. This attribute allows you to set the font family, size, and color for various elements of your plot, such as the title, axis labels, and legend.
using PlotlyJS
# Create a scatter plot
trace = scatter(; x = [1, 2, 3, 4], y = [10, 15, 13, 17])
# Set the font family and size for the title
layout = Layout(title = "My Plot", font = Font(family = "Arial", size = 18))
# Create the plot
plot(trace, layout)
In this example, we create a scatter plot and set the font family to “Arial” and the font size to 18 for the plot title. You can customize the font for other elements by specifying the corresponding attributes in the `font` object.
Option 2: Using CSS styling
Another way to customize the fonts in your plotly plots is by using CSS styling. PlotlyJS allows you to add custom CSS styles to your plots, which gives you more flexibility in terms of font customization.
using PlotlyJS
# Create a scatter plot
trace = scatter(; x = [1, 2, 3, 4], y = [10, 15, 13, 17])
# Add custom CSS styles
css = """
.plot-title {
font-family: Arial;
font-size: 18px;
}
"""
# Create the plot with custom CSS styles
plot(trace, css = css)
In this example, we create a scatter plot and define a custom CSS style for the plot title. We set the font family to “Arial” and the font size to 18 pixels. You can add more CSS styles to customize other elements of your plot.
Option 3: Using external fonts
If the available fonts in plotly do not meet your requirements, you can use external fonts by specifying the font URL in your Julia code. This allows you to use any font available on the web in your plotly plots.
using PlotlyJS
# Create a scatter plot
trace = scatter(; x = [1, 2, 3, 4], y = [10, 15, 13, 17])
# Specify the font URL
font_url = "https://fonts.googleapis.com/css2?family=Roboto"
# Create the plot with the external font
plot(trace, font_url = font_url)
In this example, we create a scatter plot and specify the font URL for the “Roboto” font from Google Fonts. PlotlyJS will load the font from the specified URL and use it in the plot. You can use any font available on the web by providing the corresponding font URL.
After exploring these three options, it is difficult to determine which one is better as it depends on your specific requirements and preferences. Option 1 provides a simple and straightforward way to customize the fonts using the `font` attribute. Option 2 gives you more flexibility by allowing you to use CSS styling. Option 3 allows you to use any font available on the web, but it requires specifying the font URL. Consider your needs and choose the option that best suits your project.