Pavan Kumar
Pavan Kumar

Reputation: 1

Is there a way to do equally spaces for Yaxis when y axis scales are of different values?

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.ticker as ticker
import numpy as np

class plottingCanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(plottingCanvas, self).__init__(fig)
        fig.tight_layout()

canvas = plottingCanvas(self, width=5, height=4, dpi=100)
    

canvas.axes.yaxis.grid(True, linestyle='--')
ylabel = np.array([0.000002, 0.00001, 0.00002, 0.0001, 0.0002, 0.001, 0.002, 0.005, 0.01, 0.05, 0.1, 0.2])
canvas.axes.yaxis.set_ticks(ylabel)
ticker = matplotlib.ticker.EngFormatter(unit='V')
canvas.axes.yaxis.set_major_formatter(ticker)
canvas.axes.set_ylim(ymin=0.000002, ymax=0.200)
canvas.draw()

Above is the code where y axis indicate the voltage values which range from uV to mV . With the above I'm getting the plot as below:

enter image description here

I even tried with below code but no luck .

canvas.axes.yaxis.set_ticks(np.arange(ylabel))
canvas.axes.yaxis.set_set_ticklabels(ylabel)

Can someone help in getting equally spaced y axis scale.

Thanks.

enter image description here

Expecting the equally spaced y axis scale as show in the above figure.

Upvotes: -1

Views: 44

Answers (1)

Alan
Alan

Reputation: 3042

TL;DR: You are setting the ylabel when you should be setting yticks.

Your new image is still inconsistent (unless you are using a log scale or similar). You can't have 2uV -> 10uV etc., you would need to have 2 uV -> 4 uV etc.

There are also two errors in the code you gave:

canvas = plottingCanvas(self, width=5, height=4, dpi=100) should not include self.
ticker = matplotlib.ticker.EngFormatter(unit='V') should not include matplotlib as you have used import matplotlib.ticker as ticker.

I'm not familiar with the backend_qt5agg, I'll give an example below using the regular plot and it should translate across directly.

Assuming plot values are x and y, where x is a series and y is a series, and the third value is the step value.

yticks(np.arange(min(y), max(y)+1, 3.0))

Note: The min and max values can be set manually. For example, if you like having 200 uV as the ceiling, then:

yticks(np.arange(0, 201, 1.0))

Example:

import matplotlib.pyplot as plt
import numpy as np

x = ["one", "two", three]
y = np.array([0.000002, 0.00001, 0.00002, 0.0001, 0.0002, 0.001, 0.002, 0.005, 0.01, 0.05, 0.1, 0.2])
plt.plot(x, y)
plt.xlabel('Time')
plt.ylabel('uV')
plt.yticks(np.arange(min(y), max(y)+1, 0.1))
plt.savefig('measurements.png')

Given the wide array of values in the example - min of 0.000002 and max of 0.2 - it would make some sense to round out the values. When comparing to 0.2 or 0.05, is there any difference between 0.000002 and 0.00001? They may be functionally zero.

Upvotes: 0

Related Questions