Plotly教程

Plotly 格式轴和刻度

您可以通过指定线宽和颜色来配置每个轴的外观。也可以定义网格宽度和网格颜色。让我们在本章中详细了解相同的内容。

绘制轴和刻度线

在 Layout 对象的属性中,将 showticklabels 设置为 true 将启用刻度。 tickfont 属性是一个指定字体名称的 dict 对象e、大小、颜色等。tickmode 属性可以有两个可能的值——线性和数组。如果是线性的,则起始刻度的位置由 tick0 决定,刻度之间的步进由 dtick 属性决定。
如果 tickmode 设置为数组,您必须提供值和标签列表作为 tickvalticktext 属性。
Layout 对象也有 Exponentformat 属性设置为 ‘e’ 将导致刻度值以科学计数法显示。您还需要将 showexponent 属性设置为 ‘all’
我们现在通过指定行、网格和标题字体属性以及刻度模式、值和字体来格式化上面示例中的布局对象,以配置 x 和 y 轴
layout = go.Layout(
   title = "Sine and cos",
   xaxis = dict(
      title = 'angle',
      showgrid = true,
      zeroline = true,
      showline = true,
      showticklabels = true,
      gridwidth = 1
   ),
   yaxis = dict(
      showgrid = true,
      zeroline = true,
      showline = true,
      gridcolor = '#bdbdbd',
      gridwidth = 2,
      zerolinecolor = '#969696',
      zerolinewidth = 2,
      linecolor = '#636363',
      linewidth = 2,
      title = 'VALUE',
      titlefont = dict(
         family = 'Arial, sans-serif',
         size = 18,
         color = 'lightgrey'
      ),
      showticklabels = true,
      tickangle = 45,
      tickfont = dict(
      family = 'Old Standard TT, serif',
      size = 14,
      color = 'black'
      ),
      tickmode = 'linear',
      tick0 = 0.0,
      dtick = 0.25
   )
)
带轴和刻度的绘图

多轴绘图

有时在图形中具有双 x 或 y 轴 很有用;例如,一起绘制具有不同单位的曲线时。 Matplotlib 通过 twinxtwiny 函数支持这一点。在以下示例中,该图具有 双 y 轴,一个显示 exp(x),另一个显示 log(x)
x = np.arange(1,11)
y1 = np.exp(x)
y2 = np.log(x)
trace1 = go.Scatter(
   x = x,
   y = y1,
   name = 'exp'
)
trace2 = go.Scatter(
   x = x,
   y = y2,
   name = 'log',
   yaxis = 'y2'
)
data = [trace1, trace2]
layout = go.Layout(
   title = 'Double Y Axis Example',
   yaxis = dict(
      title = 'exp',zeroline=True,
      showline = true
   ),
   yaxis2 = dict(
      title = 'log',
      zeroline = true,
      showline = true,
      overlaying = 'y',
      side = 'right'
   )
)
fig = go.Figure(data=data, layout=layout)
iplot(fig)
此处,附加 y 轴配置为 yaxis2 并显示在右侧,标题为 ‘log’。结果图如下-
多轴绘图
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4