Color bar from a list

Viewed 583

I have 500 files, in each loop I open one of them and I store the columns in lists and then generate 3 graphs. In each loop I'm superimposing the respective graphics. The code works but I can not place the error bar that depends on the "t" list (the three graphics should have the same color bar). If it were a single figure I can do it using: plt.colorbar (), as shown in the figure that I append, but I want to generate it for the 3 figures.

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

f1 = plt.figure()
f2 = plt.figure()
f3 = plt.figure()
ax1 = f1.add_subplot(111)
ax2 = f2.add_subplot(111)
ax3 = f3.add_subplot(111)

t = np.arange(0, 45000*10**5, 90*10**5)

for j in range(1, 501):

    x1, x2, x3, x4 = np.loadtxt('file' + str(j), usecols = (1,2,3,4), unpack = True)

    ax1.scatter(x1, x2, c=t, cmap=cm.spring)
    ax2.scatter(x1, x3, c=t, cmap=cm.spring)
    ax3.scatter(x1, x4, c=t, cmap=cm.spring)

f1.savefig('plot1', format = 'pdf', dpi = 1200, bbox_inches = 'tight')
f2.savefig('plot2', format = 'pdf', dpi = 1200, bbox_inches = 'tight')
f3.savefig('plot3', format = 'pdf', dpi = 1200, bbox_inches = 'tight')

I have seen some examples to place the color bar (for example, http://matplotlib.org/1.3.1/examples/pylab_examples/colorbar_tick_labelling_demo.html) but it seems to me that it is not applicable to my case, as you can see in the figure that I am attachingenter image description [enter image description here]1here, since not all the space in my graphic is full and the points can overlap (I am using transparency) due to the randomness of my data and due to the loop.

In summary, I would like to add a vertical color bar based on the "t" list. Thank you very much in advance for the help.

1 Answers

This is how you would add a vertical color bar to each plot based on the values in the array t:

import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
import cufflinks as cf

t = np.arange(0, 45000*10**5, 90*10**5)

for j in range(1, 2): # Loop just once for testing (instead of 501 times)

    # Load files
    # x1, x2, x3, x4 = np.loadtxt('file' + str(j), usecols = (1,2,3,4), unpack = True)

    # Generate sample data (instead of loading files)
    data = cf.datagen.lines(4, 500)
    x1 = data.iloc[:,0]
    x2 = data.iloc[:,1]
    x3 = data.iloc[:,2]
    x4 = data.iloc[:,3]    

    f1, ax1 = plt.subplots()
    sc1 = ax1.scatter(x1, x2, c=t, cmap=cm.spring)
    plt.colorbar(sc1)

    f2, ax2 = plt.subplots()
    sc2 = ax2.scatter(x1, x3, c=t, cmap=cm.spring)
    plt.colorbar(sc2)

    f3, ax3 = plt.subplots()
    sc3 = ax3.scatter(x1, x4, c=t, cmap=cm.spring)
    plt.colorbar(sc3)

f1.savefig('plot1.pdf', dpi = 1200, bbox_inches = 'tight')
f2.savefig('plot2.pdf', dpi = 1200, bbox_inches = 'tight')
f3.savefig('plot3.pdf', dpi = 1200, bbox_inches = 'tight')

Figure 1 Figure 2 Figure 3

Related