2

I'm trying to sort out a plot which at the moment looks like this: it's a scatterplot of a series of 17 different groups, each corresponding to a specific colour

I'm trying to figure out how to do a shift for different dx values for each dataset in such mode that the LAST item of the series remains at the center (in this case q), whereas the first 8 items (from a to h) would be shifted on the left, the last 8 to the right (from g to p), each for a different offset.

I have two arrays (M and M1) of shape (12, 17) for each item within a loop, so that shape corresponds to one color of the scatter points.

    import matplotlib.cm as cm
    import matplotlib
    import matplotlib.pyplot as plt
    import numpy as np

    fig, ax0 = plt.subplots (nrows=1, ncols=1)

    months_expanded = np.zeros((12,17))  
    months = np.arange(1, 13)
    M  = np.random.rand(12,17)
    M1 = np.random.rand(12,17)
    datalist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q']

    for idx, sub in enumerate(datalist):
        for i in range(0,12):
            months_expanded[i] = np.repeat(months[i], 17)

        difference = abs(M - M1)

        ax0.scatter(months_expanded[:, idx], difference[:, idx], label = sub)                       

        colormap = plt.cm.gist_ncar                                                 
        colorst = [colormap(i) for i in    np.linspace(0,0.9,len(ax0.collections))]       
        for t,j1 in enumerate(ax0.collections):
            j1.set_color(colorst[t])

       ax0.legend(loc='center left', bbox_to_anchor=(1, 0.5))
       ax0.yaxis.grid(True)
       ax0.xaxis.grid(True)

       matplotlib.rcParams.update({'font.size': 30})
    fig.set_size_inches(20,20)
    fig.savefig(outfile, bbox_inches='tight')
PEBKAC
  • 748
  • 1
  • 9
  • 28

1 Answers1

2

You can predefine an offset and create a set of x-values that include the offset. See the below minimal example that illustrates this point:

import numpy as np
import matplotlib.pyplot as plt

N = 5 # This would be 17 in your case

# Create offsets
dx = np.linspace(-0.5, 0.5, N)
# Put last data set in centre
dx[N//2:] += (dx[1]-dx[0])
dx[-1] = 0.0
dx_expanded = np.repeat(dx, 12).reshape(N, 12)

# X-values to be shifted
months = np.arange(1, 13)
months_expanded = np.tile(months, N).reshape(N, 12)
months_shifted = months_expanded + dx_expanded

# Y-values
M  = np.random.rand(12,N)
M = np.linspace(1, 5, 12)
M = np.tile(M, N).reshape(N,12)

# Plot results
for idx in range(N):
  plt.scatter(months_shifted[idx], M[idx])
plt.show()

Result: enter image description here

MPA
  • 1,878
  • 2
  • 26
  • 51
  • thank you very much, I still have to figure it out for my dataset but anyway I really appreciate your help! – PEBKAC May 22 '18 at 10:57