Creating custom tick labels in a plot

I have been trying to create a contour plot. The data set comprises of 366 units in the X-axis and 216 units in the Y-axis. Hence the axes display values from 0 to 365 and 0 to 215 in the X and Y axes respectively.

  1. I want to label the X-axis to display equally spaced names of 12 months. ie., Jan, Feb, …, Dec, and Y-axis to show only 3 points labelled -30, 0 and 30.

  2. I want to create a line through the points of maxima.

How can I do these?

@navajyoth814 To place the ticklabels as you want, insert in the dict xaxis the following key-value pairs:

tickvals=[(2*k-1)*hx/2 for k in range(1,13)]
ticktext=['January', 'February', ...]

where hx=366/12.0 was defined before.

For yaxis set:

tickvals=[k*hy for k in range(3)]
ticktext=[-30, 0, 30]

with hy=266.0/2.

1 Like

Thank you much for the reply. It helped!

What I’ve been able to chalk up so far has been added below for your reference.

But, I still have no idea how to proceed with the second problem of creating a line connecting points of maxima in the same contour plot.

@navajyoth814 Just add one more trace of type β€˜scatter’, mode=β€˜lines’, that defines the line of interest: https://plot.ly/python/line-charts/

Again, not enough context in the answers.

@Soerendip

When you don’t want to display as tick labels the values of your data, then plotly provides a β€œtrick”, namely to set
xaxis_tickvals, xaxis_ticktext. The former is the list of new values (positions) at which you want to display the corresponding strings/values in the list xaxis_ticktext.

In the above example we had:

tickvals = [(2*k-1)*hx/2 for k in range(1,13)]
ticktext= [datetime.date(2019, m, 1).strftime('%B') for m in range(1, 13)]

where

tickvals = [15.25, 45.75, 76.25, 106.75, 137.25, 167.75, 198.25, 228.75, 259.25, 289.75, 320.25, 350.75]
ticktext = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

For more context :slight_smile: you can read plotly docs: https://plot.ly/python/reference/#layout-xaxis-tickvals .

1 Like