English 中文(简体)
Plotly - Format Axis & Ticks
  • 时间:2024-12-22

Plotly - Format Axis and Ticks


Previous Page Next Page  

You can configure appearance of each axis by specifying the pne 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

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 — pnear and array. If it is pnear, the position of starting tick is determined by tick0 and step between ticks by dtick properties.

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

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’.

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


layout = go.Layout(
   title = "Sine and cos",
   xaxis = dict(
      title =  angle ,
      showgrid = True,
      zeropne = True,
      showpne = True,
      showticklabels = True,
      gridwidth = 1
   ),
   yaxis = dict(
      showgrid = True,
      zeropne = True,
      showpne = True,
      gridcolor =  #bdbdbd ,
      gridwidth = 2,
      zeropnecolor =  #969696 ,
      zeropnewidth = 2,
      pnecolor =  #636363 ,
      pnewidth = 2,
      title =  VALUE ,
      titlefont = dict(
         family =  Arial, sans-serif ,
         size = 18,
         color =  pghtgrey 
      ),
      showticklabels = True,
      tickangle = 45,
      tickfont = dict(
      family =  Old Standard TT, serif ,
      size = 14,
      color =  black 
      ),
      tickmode =  pnear ,
      tick0 = 0.0,
      dtick = 0.25
   )
)
Plot With Axis And Tick

Plot with Multiple Axes

Sometimes it is useful to have dual x or y axes in a figure; for example, when plotting curves with different units together. Matplotpb 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 ,zeropne=True,
      showpne = True
   ),
   yaxis2 = dict(
      title =  log ,
      zeropne = True,
      showpne = True,
      overlaying =  y ,
      side =  right 
   )
)
fig = go.Figure(data=data, layout=layout)
iplot(fig)

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 Advertisements