Plotly 简明教程

Plotly - Format Axis and Ticks

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

You can configure appearance of each axis by specifying the line width and color. It is also possible to define grid width and grid color. Let us learn about the same in detail in this chapter.

Plot with Axis and Tick

在布局对象的属性中,将 showticklabels 设置为 true 将启用刻度。tickfont 属性是一个 dict 对象,用于指定字体名称、大小、颜色等。tickmode 属性有两个可能的值 - 线性和数组。如果是线性,则起始刻度的坐标由 tick0 确定,而刻度之间的步长由 dtick 属性确定。

In the Layout object’s properties, setting showticklabels to true will enable ticks. The tickfont property is a dict object specifying font name, size, color, etc. The tickmode property can have two possible values — linear and array. If it is linear, the position of starting tick is determined by tick0 and step between ticks by dtick properties.

如果将 tickmode 设置为数组,则必须提供值的列表,并将其作为 tickvalticktext 属性。

If tickmode is set to array, you have to provide list of values and labels as tickval and ticktext properties.

布局对象还具有 Exponentformat 属性,将其设置为 ‘e’ 将导致刻度值以科学记数法显示。您还需要将 showexponent 属性设置为 ‘all’

The Layout object also has Exponentformat attribute set to ‘e’ will cause tick values to be displayed in scientific notation. You also need to set showexponent property to ‘all’.

现在我们在上述示例中设置布局对象,通过指定线、网格和标题字体属性以及刻度模式、值和字体来配置 x 和 y axis

We now format the Layout object in above example to configure x and y axis by specifying line, grid and title font properties and tick mode, values and font.

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
   )
)
plot with axis and tick

Plot with Multiple Axes

有时,在图形中同时使用 x or y axes 很有用;例如,在用不同单位绘制曲线时。Matplotlib 通过 twinxtwiny 函数支持此功能。在以下示例中,绘图具有 dual y axes ,一个显示 exp(x) ,另一个显示 log(x)

Sometimes it is useful to have dual x or y axes in a figure; for example, when plotting curves with different units together. Matplotlib supports this with the twinx and twiny functions. In the following example, the plot has dual y axes, one showing exp(x) and other showing 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’ 。生成的绘图如下 −

Here, additional y axis is configured as yaxis2 and appears on right side, having ‘log’ as title. Resultant plot is as follows −

plot with multiple axes