-1

Is there any way in matplotlib to keep tick locations evenly whereas keeping their values uneven so that data may squeeze some interval and may expand at another. For example following code generates sine wave with ticks [0.0,0.5,1.0,1.5,2.0]

import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
ax = plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.autoscale(False)
ax.xaxis.set_ticks([0.0,0.5,1.0,1.5,2.0])
plt.show()

I want to change the value 0.5 to 0.25 at ax.xaxis.set_ticks([0.0,0.5,1.0,1.5,2.0]) but keep it in the same location on the plot.

ERD
  • 1
  • 2
  • You need to provide at least a hand-draw mockup of what you are trying to achieve. As the answer below demonstrates, what you want is entirely unclear. – Diziet Asahi Sep 10 '17 at 12:09
  • Is this https://stackoverflow.com/questions/32185411/break-in-x-axis-of-matplotlib what you are looking for? Plot from 0–0.25 on 1/4 of the x-axis, and from 0.25–2.0 on the remaining 3/4? – Diziet Asahi Sep 10 '17 at 12:12

2 Answers2

0

Apparently the following is not what OP is asking for. I will leave it here until the question is edited, such that people at least understand what is not desired.

You can add set_ticklabels to label the ticks differently.

ax.xaxis.set_ticks(     [0.0, 0.50, 1.0,1.5,2.0])
ax.xaxis.set_ticklabels([0.0, 0.25, 1.0,1.5,2.0])

Comlpete example:

import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
ax = plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.autoscale(False)
ax.xaxis.set_ticks([0.0,0.5,1.0,1.5,2.0])
ax.xaxis.set_ticklabels([0.0,0.25,1.0,1.5,2.0])
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • My intention is not changing the label but only the value of the tick and expected to data be plotted according to tick values. – ERD Sep 10 '17 at 08:54
  • That is not entirely clear. Where should e.g. 0.4 or 0.6 located then? You would want to explain what you want to achieve much more in detail in the question then. – ImportanceOfBeingErnest Sep 10 '17 at 11:01
  • What I want to achieve actually, when I change the tick value I want matplotlib not to change the position of the tick rather than that change the way that it plots the data. For example, for the sine wave I want to change value 0.5 with 0.25 and want to keep that value in place of 0.5. By doing so I would expect matplotlib to plot sin wave according to the tick interval not with autospacing. – ERD Sep 10 '17 at 11:49
  • As I said, as I do not understand what you want then, you would need to edit your question to explain that better. Also mind the comments below the question. – ImportanceOfBeingErnest Sep 10 '17 at 13:18
0

I was working with something similar.

I think that what you wanted to do is the following:

ax.set_xticks((0,0.25,1,1.5,2)) # makes ticks values uneven
ax.xaxis.set_minor_locator(plt.MultipleLocator(0.25)) # locates ticks at a multiple of the number you provide, as here 0.25 (keeps ticks evenly spaced)
bhristov
  • 3,137
  • 2
  • 10
  • 26