Python Forum
Matplotlib scatter plot in loop with None values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Matplotlib scatter plot in loop with None values (/thread-35444.html)



Matplotlib scatter plot in loop with None values - ivan_sc - Nov-03-2021

Hello,

As part of a more complex project, I am trying to plot multiple curves sharing a common X axis.
However, each curve could have some None values for some X values.

When making the scatter plots through a FOR loop, I get a pretty strange result: if the first curve has None values after the fifth value, then the scatter plot of the second curve stops after the fifth value too. While the normal plot does not.

Does any of you have a suggestion?

Thanks!

import os, time, tempfile, sys
os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
import matplotlib.pyplot as plt
import numpy as np
import mpld3


table = []
x = list(range(0,11))
y1 = [-90.,-91.,-87,-95,-90,-91.,None,None,None,None,None]
y2 = [-10.,-15.,-30.,-5.,-8.,-10.,-15.,-30.,-5.,-8.,0.]
table.append(x)
table.append(y1)
table.append(y2)

myDpi = 100
lineWidth = 1.
axisFontSize = 10
yAxisTitleFontSize = 10
alphaScatter = 0.5
fig, ax = plt.subplots(figsize=(1920/myDpi, 1000/myDpi), dpi=myDpi)
#plt.tight_layout()
plt.ylabel("[%]", fontsize=yAxisTitleFontSize)
ax.grid(axis='y')
ax.tick_params(axis='both', which='major', labelsize=axisFontSize)

for i in range(len(table)-1):
	line = ax.plot(table[0], table[i+1], linewidth=lineWidth)
	scatter = ax.scatter(table[0], table[i+1], alpha=alphaScatter)
	labels = ['Curve ' + str(i+1) for j in range(11)]
	mpld3.plugins.connect(fig, mpld3.plugins.PointLabelTooltip(scatter, labels=labels))

mpld3.save_html(fig,'myGraph.html') 
[attachment=1364]


RE: Matplotlib scatter plot in loop with None values - jefsummers - Nov-04-2021

The problem appears to be in mpld3. I don't have that, so I removed all references to it. Ran the script (in Colab) and got all the dots