如何检查对象是否为“matplotlib.collectives.PolyCollection”类型

gt0wga4j  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(86)

对于一个特定的任务(Link),我想检查一个对象是否是:

  1. matplotlib.collections.PolyCollection

字符串
或a:

  1. matplotlib.lines.Line2D


对象。
我把它累成这样:

  1. if isinstance(handle, matplotlib.collections.PolyCollection):


但这并不奏效。如果要测试两个变量h和句柄是否属于同一类型,我如何检查它们是否都是 matplotlib.collections.PolyCollectionmatplotlib.lines.Line2D 对象?

编辑1

下面是有问题的代码,它适应了上面链接中的解决方案:

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import matplotlib.mlab as mlab
  4. import math
  5. def is_inlist(handle, handles):
  6. for h in handles:
  7. if h.get_color() == handle.get_color() and \
  8. h.get_linestyle() == handle.get_linestyle() and \
  9. h.get_marker() == handle.get_marker():
  10. return True
  11. return False
  12. lines=[]
  13. labels=[]
  14. legend_properties = {'weight':'bold','size':10}
  15. # Example data
  16. mu = 0
  17. mu2 = 5
  18. variance = 1
  19. variance2 = 2
  20. sigma = math.sqrt(variance)
  21. sigma2 = math.sqrt(variance2)
  22. x = np.linspace(mu-3*variance,mu+3*variance, 100)
  23. x2 = np.linspace(mu2-3*variance2,mu2+3*variance2, 100)
  24. nrows = 4
  25. # Plot
  26. fig, axis = plt.subplots(nrows, sharex=True, sharey=False, figsize=(5, 8))
  27. fig.subplots_adjust(hspace=0.0001)
  28. #fig.suptitle("Stacked Plots with global Legend which contains to little elements",fontsize=14,weight='bold')
  29. axis[0].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  30. axis[0].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='orange',alpha=0.5,label="PEAK2", interpolate=True)
  31. axis[0].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  32. axis[0].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  33. axis[0].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  34. axis[1].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  35. axis[1].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='purple',alpha=0.5,label="PEAK2", interpolate=True)
  36. axis[1].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  37. axis[1].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  38. axis[1].fill_between(x+6.5,0,mlab.normpdf(x, mu, sigma), color='yellow',alpha=0.5,label="PEAK5", interpolate=True)
  39. axis[1].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  40. axis[2].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  41. axis[2].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='orange',alpha=0.5,label="PEAK2", interpolate=True)
  42. axis[2].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='#73d216',alpha=0.5,label="PEAK3", interpolate=True)
  43. axis[2].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  44. axis[2].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  45. axis[3].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  46. axis[3].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='purple',alpha=0.5,label="PEAK2", interpolate=True)
  47. axis[3].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  48. axis[3].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  49. axis[3].fill_between(x+6.5,0,mlab.normpdf(x, mu, sigma), color='#73d216',alpha=0.5,label="PEAK5", interpolate=True)
  50. axis[3].fill_between(x+5.5,0,mlab.normpdf(x, mu, sigma), color='violet',alpha=0.5,label="PEAK6", interpolate=True)
  51. axis[3].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  52. for i in range(nrows):
  53. h, l = axis[i].get_legend_handles_labels()
  54. for hi, li in zip(h,l):
  55. if not is_inlist(hi, lines):
  56. lines.append(hi)
  57. labels.append(li)
  58. #x for x in item if x not in Z
  59. # only 3 Legend entrys Label1 , Label2 and Label3 are visible .. Differences in cloors and markers are ignored
  60. plt.legend(handles=lines, labels=labels,bbox_to_anchor=(0., nrows+.02, 1., .102), loc=3,ncol=3, prop=legend_properties,mode="expand", borderaxespad=0.,frameon=False,framealpha=0.0)
  61. plt.show()


不幸的是,它给了我错误:

  1. Traceback (most recent call last):
  2. File "PATH..../.py", line 76, in <module>
  3. if not is_inlist(hi, lines):
  4. File "PATH..../.py", line 9, in is_inlist
  5. if h.get_color() == handle.get_color() and \
  6. AttributeError: 'PolyCollection' object has no attribute 'get_color'


有人建议我为每种类型的matplotlib对象做一个案例分析。这就是我奋斗的地方。我想改变“is_inlist”函数,以适应不同的情况。但案例分析本身还不起作用:

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import matplotlib.mlab as mlab
  4. import math
  5. def is_inlist(handle, handles):
  6. for h in handles:
  7. if isinstance(handle, matplotlib.collections.PolyCollection) and isinstance(h, matplotlib.collections.PolyCollection):
  8. if h.get_color() == handle.get_color() and \
  9. h.get_linestyle() == handle.get_linestyle() and \
  10. h.get_marker() == handle.get_marker():
  11. return True
  12. if isinstance(handle, matplotlib.lines.Line2D) and isinstance(h, matplotlib.lines.Line2D):
  13. if h.get_color() == handle.get_color() and \
  14. h.get_linestyle() == handle.get_linestyle() and \
  15. h.get_marker() == handle.get_marker():
  16. return True
  17. return False
  18. lines=[]
  19. labels=[]
  20. legend_properties = {'weight':'bold','size':10}
  21. # Example data
  22. mu = 0
  23. mu2 = 5
  24. variance = 1
  25. variance2 = 2
  26. sigma = math.sqrt(variance)
  27. sigma2 = math.sqrt(variance2)
  28. x = np.linspace(mu-3*variance,mu+3*variance, 100)
  29. x2 = np.linspace(mu2-3*variance2,mu2+3*variance2, 100)
  30. nrows = 4
  31. # Plot
  32. fig, axis = plt.subplots(nrows, sharex=True, sharey=False, figsize=(5, 8))
  33. fig.subplots_adjust(hspace=0.0001)
  34. #fig.suptitle("Stacked Plots with global Legend which contains to little elements",fontsize=14,weight='bold')
  35. axis[0].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  36. axis[0].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='orange',alpha=0.5,label="PEAK2", interpolate=True)
  37. axis[0].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  38. axis[0].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  39. axis[0].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  40. axis[1].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  41. axis[1].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='purple',alpha=0.5,label="PEAK2", interpolate=True)
  42. axis[1].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  43. axis[1].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  44. axis[1].fill_between(x+6.5,0,mlab.normpdf(x, mu, sigma), color='yellow',alpha=0.5,label="PEAK5", interpolate=True)
  45. axis[1].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  46. axis[2].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  47. axis[2].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='orange',alpha=0.5,label="PEAK2", interpolate=True)
  48. axis[2].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='#73d216',alpha=0.5,label="PEAK3", interpolate=True)
  49. axis[2].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  50. axis[2].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  51. axis[3].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  52. axis[3].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='purple',alpha=0.5,label="PEAK2", interpolate=True)
  53. axis[3].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  54. axis[3].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  55. axis[3].fill_between(x+6.5,0,mlab.normpdf(x, mu, sigma), color='#73d216',alpha=0.5,label="PEAK5", interpolate=True)
  56. axis[3].fill_between(x+5.5,0,mlab.normpdf(x, mu, sigma), color='violet',alpha=0.5,label="PEAK6", interpolate=True)
  57. axis[3].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  58. for i in range(nrows):
  59. h, l = axis[i].get_legend_handles_labels()
  60. for hi, li in zip(h,l):
  61. if not is_inlist(hi, lines):
  62. lines.append(hi)
  63. labels.append(li)
  64. # only 3 Legend entrys Label1 , Label2 and Label3 are visible .. Differences in cloors and markers are ignored
  65. plt.legend(handles=lines, labels=labels,bbox_to_anchor=(0., nrows+.02, 1., .102), loc=3,ncol=3, prop=legend_properties,mode="expand", borderaxespad=0.,frameon=False,framealpha=0.0)
  66. plt.show()


我得到的错误是:

  1. Traceback (most recent call last):
  2. File "Path/.. .py", line 84, in <module>
  3. if not is_inlist(hi, lines):
  4. File "Path/.. .py", line 9, in is_inlist
  5. if isinstance(handle, matplotlib.collections.PolyCollection) and isinstance(handle, matplotlib.collections.PolyCollection):
  6. NameError: global name 'matplotlib' is not defined

编辑2

我补充道:

  1. import matplotlib.collections


正如我所建议的

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import matplotlib.mlab as mlab
  4. import math
  5. import matplotlib.collections
  6. def is_inlist(handle, handles):
  7. for h in handles:
  8. if isinstance(handle, matplotlib.collections.PolyCollection) and isinstance(h, matplotlib.collections.PolyCollection):
  9. if h.get_facecolor() == handle.get_facecolor() and \
  10. h.get_linestyle() == handle.get_linestyle() and \
  11. h.get_alpha() == handle.get_alpha():
  12. return True
  13. if isinstance(handle, matplotlib.lines.Line2D) and isinstance(h, matplotlib.lines.Line2D):
  14. if h.get_color() == handle.get_color() and \
  15. h.get_linestyle() == handle.get_linestyle() and \
  16. h.get_marker() == handle.get_marker():
  17. return True
  18. return False
  19. lines=[]
  20. labels=[]
  21. legend_properties = {'weight':'bold','size':10}
  22. # Example data
  23. mu = 0
  24. mu2 = 5
  25. variance = 1
  26. variance2 = 2
  27. sigma = math.sqrt(variance)
  28. sigma2 = math.sqrt(variance2)
  29. x = np.linspace(mu-3*variance,mu+3*variance, 100)
  30. x2 = np.linspace(mu2-3*variance2,mu2+3*variance2, 100)
  31. nrows = 4
  32. # Plot
  33. fig, axis = plt.subplots(nrows, sharex=True, sharey=False, figsize=(5, 8))
  34. fig.subplots_adjust(hspace=0.0001)
  35. #fig.suptitle("Stacked Plots with global Legend which contains to little elements",fontsize=14,weight='bold')
  36. axis[0].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  37. axis[0].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='orange',alpha=0.5,label="PEAK2", interpolate=True)
  38. axis[0].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  39. axis[0].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  40. axis[0].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  41. axis[1].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  42. axis[1].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='purple',alpha=0.5,label="PEAK2", interpolate=True)
  43. axis[1].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  44. axis[1].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  45. axis[1].fill_between(x+6.5,0,mlab.normpdf(x, mu, sigma), color='yellow',alpha=0.5,label="PEAK5", interpolate=True)
  46. axis[1].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  47. axis[2].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  48. axis[2].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='orange',alpha=0.5,label="PEAK2", interpolate=True)
  49. axis[2].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='#73d216',alpha=0.5,label="PEAK3", interpolate=True)
  50. axis[2].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  51. axis[2].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  52. axis[3].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  53. axis[3].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='purple',alpha=0.5,label="PEAK2", interpolate=True)
  54. axis[3].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  55. axis[3].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  56. axis[3].fill_between(x+6.5,0,mlab.normpdf(x, mu, sigma), color='#73d216',alpha=0.5,label="PEAK5", interpolate=True)
  57. axis[3].fill_between(x+5.5,0,mlab.normpdf(x, mu, sigma), color='violet',alpha=0.5,label="PEAK6", interpolate=True)
  58. axis[3].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  59. for i in range(nrows):
  60. h, l = axis[i].get_legend_handles_labels()
  61. for hi, li in zip(h,l):
  62. if not is_inlist(hi, lines):
  63. lines.append(hi)
  64. labels.append(li)
  65. # only 3 Legend entrys Label1 , Label2 and Label3 are visible .. Differences in cloors and markers are ignored
  66. plt.legend(handles=lines, labels=labels,bbox_to_anchor=(0., nrows-1+.02, 1., .102), loc=3,ncol=3, prop=legend_properties,mode="expand", borderaxespad=0.,frameon=False,framealpha=0.0)
  67. plt.show()


我现在得到的错误是:

  1. Traceback (most recent call last):
  2. File "Path/.. .py", line 80, in <module>
  3. if not is_inlist(hi, lines):
  4. File "Dath/.. .py", line 10, in is_inlist
  5. if h.get_facecolor() == handle.get_facecolor() and \
  6. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

解决方案基于ImportanceOfBeingErnest的解释:

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import matplotlib.mlab as mlab
  4. import math
  5. import matplotlib.collections
  6. def is_inlist(handle, handles):
  7. for h in handles:
  8. if isinstance(handle, matplotlib.collections.PolyCollection) and isinstance(h, matplotlib.collections.PolyCollection):
  9. if np.all(h.get_facecolor() == handle.get_facecolor()) and \
  10. np.all(h.get_linestyle() == handle.get_linestyle()) and \
  11. np.all(h.get_alpha() == handle.get_alpha()):
  12. return True
  13. elif isinstance(handle, matplotlib.lines.Line2D) and isinstance(h, matplotlib.lines.Line2D):
  14. if h.get_color() == handle.get_color() and \
  15. h.get_linestyle() == handle.get_linestyle() and \
  16. h.get_marker() == handle.get_marker():
  17. return True
  18. return False
  19. lines=[]
  20. labels=[]
  21. legend_properties = {'weight':'bold','size':10}
  22. # Example data
  23. mu = 0
  24. mu2 = 5
  25. variance = 1
  26. variance2 = 2
  27. sigma = math.sqrt(variance)
  28. sigma2 = math.sqrt(variance2)
  29. x = np.linspace(mu-3*variance,mu+3*variance, 100)
  30. x2 = np.linspace(mu2-3*variance2,mu2+3*variance2, 100)
  31. nrows = 4
  32. # Plot
  33. fig, axis = plt.subplots(nrows, sharex=True, sharey=False, figsize=(5, 8))
  34. fig.subplots_adjust(hspace=0.0001)
  35. #fig.suptitle("Stacked Plots with global Legend which contains to little elements",fontsize=14,weight='bold')
  36. axis[0].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  37. axis[0].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='orange',alpha=0.5,label="PEAK2", interpolate=True)
  38. axis[0].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  39. axis[0].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  40. axis[0].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  41. axis[1].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  42. axis[1].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='purple',alpha=0.5,label="PEAK2", interpolate=True)
  43. axis[1].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  44. axis[1].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  45. axis[1].fill_between(x+6.5,0,mlab.normpdf(x, mu, sigma), color='yellow',alpha=0.5,label="PEAK5", interpolate=True)
  46. axis[1].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  47. axis[2].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  48. axis[2].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='orange',alpha=0.5,label="PEAK2", interpolate=True)
  49. axis[2].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='#73d216',alpha=0.5,label="PEAK3", interpolate=True)
  50. axis[2].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  51. axis[2].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  52. axis[3].fill_between(x+6,0,mlab.normpdf(x, mu, sigma), color='green',alpha=0.5,label="PEAK1", interpolate=True)
  53. axis[3].fill_between(x+4,0,mlab.normpdf(x, mu, sigma), color='purple',alpha=0.5,label="PEAK2", interpolate=True)
  54. axis[3].fill_between(x+3,0,mlab.normpdf(x, mu, sigma), color='blue',alpha=0.5,label="PEAK3", interpolate=True)
  55. axis[3].fill_between(x+7,0,mlab.normpdf(x, mu, sigma), color='red',alpha=0.5,label="PEAK4", interpolate=True)
  56. axis[3].fill_between(x+6.5,0,mlab.normpdf(x, mu, sigma), color='#73d216',alpha=0.5,label="PEAK5", interpolate=True)
  57. axis[3].fill_between(x+5.5,0,mlab.normpdf(x, mu, sigma), color='violet',alpha=0.5,label="PEAK6", interpolate=True)
  58. axis[3].plot(x2,2.5*mlab.normpdf(x2, mu2, sigma2),color='black',linestyle="",label="Exp", marker="o", markersize=4)
  59. for i in range(nrows):
  60. h, l = axis[i].get_legend_handles_labels()
  61. for hi, li in zip(h,l):
  62. if not is_inlist(hi, lines):
  63. lines.append(hi)
  64. labels.append(li)
  65. # only 3 Legend entrys Label1 , Label2 and Label3 are visible .. Differences in cloors and markers are ignored
  66. plt.legend(handles=lines, labels=labels,bbox_to_anchor=(0., nrows-1+.02, 1., .102), loc=3,ncol=3, prop=legend_properties,mode="expand", borderaxespad=0.,frameon=False,framealpha=0.0)
  67. plt.show()

wgx48brx

wgx48brx1#

最初问题的解决方案是实际导入为比较提供类的模块。
你只是缺少import matplotlib.collections
下一个错误其实是不言自明的。它说不可能比较两个数组。
所以我们假设
h的面颜色为[[ 0., 0.50196078, 0., 0.5]],并且
handle的面色为[[ 1., 0.64705882, 0., 0.5]],则
h.get_facecolor() == handle.get_facecolor()的结果为[[False False True True]]
现在,两次假和两次真是真还是假?谁也不知道。因此,您需要使用any()all()来决定是要知道是否有任何元素为True,还是所有元素都为True。
在这里,您可能希望检查是否有相同的颜色,因此使用all

  1. np.all(h.get_facecolor() == handle.get_facecolor())

字符串

相关问题