0

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?

kino
  • 41
  • 4

2 Answers2

0

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?

Dylan_w
  • 472
  • 5
  • 19
  • the animation that i thought is plot the scatters one by one and create a kind of video – kino May 22 '19 at 07:37
0

this should do:

fig, axs = plt.subplots(1,3, figsize=(12,3))
for i in range(0,len(final)):
    im = axs[i].scatter(final[i]['x'],final[i]['y'],c=final[i]['value'])
fig.colorbar(im, ax=axs.ravel().tolist())

enter image description here more ideas in this thread: Matplotlib 2 Subplots, 1 Colorbar

For animation - need more details.

Poe Dator
  • 4,535
  • 2
  • 14
  • 35