Home > OS >  All scatter points aren't displayed when plotting two sets of data
All scatter points aren't displayed when plotting two sets of data

Time:11-08

I am trying to scatter two datasets on the same graph. When I try to do this, some of the data gets cut off. Here is my code:

Data = {
    "Object A": [
        [8.5, 8.2, 8.9, 10.9, 7.8, 8.3],
        [42.2, 43.7, 47.3, 44.3, 44.9, 48.2]
    ],
  "Object B": [
    [7.0, 8.8, 10.5, 9.6, 8.5, 8.2],
    [44.1, 43.1, 41.9, 43.5, 42.2, 43.7]
  ],
}

for y in Data.values():
    plt.scatter(y[0], y[1])

plt.legend(['Object A', 'Object B'])
plt.show()

And here is the result:

Image of chart

As you can see, the only the first four values of Object A are displayed on the graph. This persists even if I change its length. When I made Object B the first item in the Data list to see if that would change anything, the graph refused to display the last 2 items of Object B that time.

Image of chart attempt 2

How do I make the graph display all values? Thanks

Edit: If I add more datasets, the graph displays all of those points, and still only cuts off the last two of the very first one

CodePudding user response:

  • All of the datapoints are being plotted, but as you can see in the following plots, there are two overlapping datapoints (8.5, 42.2) and (8.2, 43.7).
  • Set alpha=0.5 to show overlapping points
  • Tested in python 3.8.12, matplotlib 3.4.3
Data = {'Object A': [[8.5, 8.2, 8.9, 10.9, 7.8, 8.3], [42.2, 43.7, 47.3, 44.3, 44.9, 48.2]],
        'Object B': [[7.0, 8.8, 10.5, 9.6, 8.5, 8.2], [44.1, 43.1, 41.9, 43.5, 42.2, 43.7]]}

fig, axes = plt.subplots(2, 2, figsize=(10, 10), sharex=True, sharey=True)
axes = axes.flat

for k, v in Data.items():
    axes[0].scatter(v[0], v[1], label=k, alpha=0.5)
    
axes[2].scatter(Data['Object A'][0], Data['Object A'][1], label='Object A')
axes[3].scatter(Data['Object B'][0], Data['Object B'][1], color='tab:orange', label='Object B')

axes[0].legend()
axes[2].legend()
axes[3].legend()
axes[0].grid()
axes[2].grid()
axes[3].grid()

fig.delaxes(axes[1])
plt.show()

enter image description here

  • Related