How to do it...

We are creating our own data in our Python module by assigning lists with data to the xValues and yValues variables.

In many graphs, the beginning of the x and y coordinate system starts at (0, 0). This is usually a good idea, so let's adjust our chart coordinate code accordingly.

Let's modify the code to set limits on both the x and y dimensions:

Matplotlib_labels_two_charts_scaled_dynamic.py

    xValues  = [1,2,3,4] 

yValues0 = [6,7.5,8,7.5]
yValues1 = [5.5,6.5,50,6] # one very high value (50)
yValues2 = [6.5,7,8,7]

axis.set_ylim(0, 8) # lower limit (0)
axis.set_xlim(0, 8) # use same limits for x

Now that we have set the same limits for x and y, our chart might look more balanced. When we run the modified code, we get the following result:

Maybe starting at (0, 0) was not such a great idea after all...

What we really want to do is adjust our chart dynamically according to the range of the data, while at the same time restricting the values that are too high or too low.

We can do this by parsing all the data to be represented in the chart while, at the same time, setting some explicit limits.

Modify the code as follows:

Matplotlib_labels_two_charts_scaled_dynamic.py

    xValues  = [1,2,3,4] 

yValues0 = [6,7.5,8,7.5]
yValues1 = [5.5,6.5,50,6] # one very high value (50)
yValues2 = [6.5,7,8,7]
yAll = [yValues0, yValues1, yValues2] # list of lists

# flatten list of lists retrieving minimum value
minY = min([y for yValues in yAll for y in yValues])

yUpperLimit = 20
# flatten list of lists retrieving max value within defined limit
maxY = max([y for yValues in yAll for y in yValues if y <
yUpperLimit])

# dynamic limits
axis.set_ylim(minY, maxY)
axis.set_xlim(min(xValues), max(xValues))

t0, = axis.plot(xValues, yValues0)
t1, = axis.plot(xValues, yValues1)
t2, = axis.plot(xValues, yValues2)

Running the code results in the following chart. We adjusted both its x and y dimensions dynamically. Note how the y-dimension now starts at 5.5 instead of 5.0, as it did before. The chart also no longer starts at (0, 0), giving us more valuable information about our data:

We are creating a list of lists for the y-dimension data and then using a list comprehension wrapped into a call to Python's min() and max() functions.

If list comprehensions seem to be a little bit advanced, what they basically are is a very compressed loop.

They are also designed to be faster than a regular programming loop.

In the preceding Python code, we created three lists that hold the y-dimensional data to be plotted. We then created another list that holds those three lists, which creates a list of lists, as follows:

    yValues0 = [6,7.5,8,7.5] 
yValues1 = [5.5,6.5,50,6] # one very high value (50)
yValues2 = [6.5,7,8,7]
yAll = [yValues0, yValues1, yValues2] # list of lists

We are interested in getting both the minimum value of all of the y-dimensional data and the maximum value contained within these three lists.

We can do this via a Python list comprehension:

    # flatten list of lists retrieving minimum value 
minY = min([y for yValues in yAll for y in yValues])

After running the list comprehension, minY is 5.5.

The preceding  line of code is the list comprehension that runs through all the values of all the data contained within the three lists and finds the minimum value using the Python min keyword.

In the very same pattern, we find the maximum value contained in the data we wish to plot. This time, we'll also set a limit within our list comprehension, which ignores all the values that are above the limit we specified, as follows:

    yUpperLimit = 20 
# flatten list of lists retrieving max value within defined limit
maxY = max([y for yValues in yAll for y in yValues if y <
yUpperLimit])

After running the preceding code with our chosen restriction, maxY has the value of 8 (not 50).

We applied a restriction to the max value, according to a predefined condition choosing 20 as the maximum value to be displayed in the chart.

For the x-dimension, we simply called min() and max() in the Matplotlib method to scale the limits of the chart dynamically.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.51.153