首页 > 解决方案 > 使用 plotly 更改图表上的字体

问题描述

我正在用 plotly 绘制一些情节。所以一切都很好,但我对字体外观不满意。即在传说中我有一种字体,而对于 x 和 y 轴我有其他字体。那么任何人都可以帮助如何解决这个问题并使用与图例中的字体相同的字体吗?

 x1 = lcBR$p
    y1 = lcBR$L
    
    x2 = lcAR$p
    y2 = lcAR$L
   
    
    fig <- plot_ly(x = ~ x1, y = ~ x1, name = '45º', type = 'scatter', mode = 'lines',
                   line = list(color = "black", width = 2)) 
    
    fig <- fig %>% add_trace(x = ~ x1,  y = ~ y1, name = 'Before reform', mode = 'lines',
                             line = list(color = "red", width = 2, dash = "solid"))
    fig <- fig %>% add_trace(x ~ x2, y = ~ y2, name = 'After reform', mode = 'lines',
                             line = list(color = "blue", width = 2, dash = "dash"))
           
    f <- list(
      family = "Courier New, monospace",
      size = 18,
      color = "#7f7f7f")
    
    x <- list(
      title = "Share of Population",
      titlefont = f)
    
    y <- list(
      title = "Share of income",
      titlefont = f)
  
    fig <- fig %>% layout(xaxis = x, yaxis = y)
    
    fig

在此处输入图像描述

标签: rplotly

解决方案


您的代码看起来与 plotly 中的“普通”python 代码看起来有些不同;这是R代码吗?但是,您可以使用它fig.update_layout来更新整个图形的布局并fig.update_xaxes更改 x 轴的外观,包括文本;看看https://plotly.com/python/figure-labels/#global-and-local-font-specification


推荐阅读