Change dimension of points with their value in matplotlib

Viewed 66

Below I created a simple example of my dataset. I have 4 points and for each steps their value change. The points are plotted in x,y plane and I want their size to change with their value. There is also one other problem, each point is connected by a line and I don't want it. (I cannot use plt.scatter)

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.plot(final[i]['x'],final[i]['y'],marker='o',markersize=22)

with this I fix the dimension the line appears in, how can I remove it? If I change the markersize, it doesn't work:

for i in range(0,len(final)):
    fig, ax = plt.subplots()
    plt.plot(final[i]['x'],final[i]['y'],marker='o',markersize=final[i]['value'])

As I said before, the result I want is a plot in which there are only the points with different dimensions depending on their value.

2 Answers

Since you cannot use scatter, you need to loop over the values to use the markersize as it does not accept arrays but a scalar. Moreover, to just plot a marker, you use 'o' for a circle. I used size*5 to enlarge the circles further.

for i in range(0,len(final)):
    fig, ax = plt.subplots()
    for x, y, size in zip(final[i]['x'],final[i]['y'], final[i]['value']):
        plt.plot(x, y, 'o', markersize=size*5)

In case you want to plot them as subplots

fig, axes = plt.subplots(1,3, figsize=(9, 2))

for i in range(0,len(final)):
    for x, y, size in zip(final[i]['x'],final[i]['y'], final[i]['value']):
        axes[i].plot(x, y, 'o', markersize=size*5)
plt.tight_layout()

enter image description here

You have an argument for the line width in plt.plot graphs. Please set it to zero.

plt.plot(final[i]["x"], final[i]["y"], marker="o", markersize=22, linewidth=0)
Related