Search code examples
pythonmatplotlibanimationcolorbar

how fix colorbar with different plot


Below I created a simple example of my dataset. I have 4 points and each steps their value change. The points are plotted in x,y positions and the colors change with their value. How i can fix one colorbar useful for each plot?

import pandas as pd
import matplotlib.pyplot as plt

data=[[1,1,3],[1,2,1],[2,1,9],[2,2,0]]
a=pd.DataFrame(data)
a.columns=['x','y','value']

data2=[[1,1,5],[1,2,2],[2,1,1],[2,2,3]]
b=pd.DataFrame(data2)
b.columns=['x','y','value']

data3=[[1,1,15],[1,2,7],[2,1,4],[2,2,8]]
c=pd.DataFrame(data3)
c.columns=['x','y','value']

final=[a,b,c]

for i in range(0,len(final)):
    fig, ax = plt.subplots()
    plt.scatter(final[i]['x'],final[i]['y'],c=final[i]['value'])
    plt.colorbar()

I have one other question, I want to create an animation of these 3 plots (with the same colorbar) but i'm not able to do that, someone can help me?


Solution

  • For the same colorbar add simply: vmin and vmax to plt.scatter. For example:

    for i in range(0,len(final)):
        fig, ax = plt.subplots()
        plt.scatter(final[i]['x'],final[i]['y'],c=final[i]['value'],vmin=0, vmax=15,)
        plt.colorbar()
    

    What kind of animation do you desire? Plot the scatters one by one?