Diff for "Cookbook/Matplotlib/Plotting values with masked arrays"

Differences between revisions 1 and 2

From time to time one might end up with "meaningless" data in an array. Be it because a detector didn't work properly or for an other reason. Or one has to deal with data in completely different ranges. In both cases plotting all values will screw up the plot. This brief example script addresses this problem and show one possible solution using masked arrays. See 'masked_demo.py' in the matplotlib examples for a reference, too.

```   1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 y_values = [0,0,100,97,98,0,99,101,0,102,99,105,101]
5 x_values = [0,1,2,3,4,5,6,7,8,9,10,11,12]
6
7 #give a threshold
8 threshold = 1
9
10 #prepare for masking arrays - 'conventional' arrays won't do it
11 y_values = np.ma.array(y_values)
12 #mask values below a certain threshold
14
15 #plot all data
17 plt.subplot(311)
18 plt.plot(x_values, y_values,'ko')
19 plt.title('All values')
20 plt.subplot(312)
23 ax = plt.subplot(313)
25 #for otherwise the range of x_values gets truncated:
26 ax.set_xlim(x_values[0], x_values[-1])
27 plt.title('Plot without masked values -\nwith full range x-axis')
28
```

The resulting figure might illustrate the problem - note the different scales in all three subplots:

Cookbook/Matplotlib/Plotting values with masked arrays (last edited 2011-02-01 03:43:54 by UnuTbu)