matplotlib 的 marker 颜色更改

背景

  • win10, anaconda 4.8.3, python 3.8.3
  • matplotlib.pyplot图形的marker, 有时颜色更改不起作用(暂这样描述), markerfacecolor不起作用
  • 类似plt.plot(x,y,‘bo’)的办法,无法对marker的填色(markerfaceecolor) 和 边缘色 (markeredgecolor)同时更改

办法

  • 更改plt.plot(x, y, ‘bo’) 为 plt.plot(x,y, ‘o’, markeredgecolor=‘b’, markerfacecolor=‘r’, markersize=10) #方便查验
  • 可能的坑: 有的marker, markerfacecolor不起作用, 只有markeredgecolor起作用,比如 +, x, |, -, 四个方向的类似 Y 负号的图形,
  • plt.Line2D.filled_markers中的符号,都可以用face 与 edge color来定义填色和边缘色
  • plt.Line2D.markers中,除filled_markers和个别nothing的部分,也就是 unfilled_markers部分(没有直接的方法取得),第一个也适用face/edge color, 其余都不适用。
  • 有的 unfilled_markers,没有符号出现在绘图里。待进一步练习、探查。
  • 例子见下
  • `#
    #输出画图中的全部 有 填充效果的marker — filled_markers
    #输出画图中的全部 无 填充效果的marker — unfilled_markers = markers - “filled_markers + nothing”

#filled_markers, 才有makerfacecolor的效果, 还有markeredgecolor
#unfilled_markers, 只有markeredgecolor才有效果. makerfacecolor无错误提示但没有效果

#参考https://matplotlib.org/gallery/lines_bars_and_markers/marker_reference.html

import matplotlib as mpl
import matplotlib.pyplot as plt

fig, ax=plt.subplots(2,22)

x=[0,1,2,3,5,6]
y=[0,1,4,16,25,36]

filled_markers=[m for m in plt.Line2D.filled_markers]
unfilled_markers=[m for m, func in plt.Line2D.markers.items() if func != ‘nothing’ and m not in filled_markers]

#print(‘filled_markers:’, filled_markers, flush=True)
#print(‘unfilled_markers:’, unfilled_markers, flush=True)

for i_plt in range(0 ,15):
#print(‘i_plt:’, i_plt, flush=True)
ax[0][i_plt].plot(x,y, filled_markers[i_plt], markerfacecolor=‘r’, markeredgecolor=‘b’, markersize=10)

for i_plt in range(22, 44):
#print(‘i_plt-22:’, (i_plt-22), flush=True)
ax[1][i_plt-22].plot(x,y, unfilled_markers[i_plt-22], markerfacecolor=‘r’, markeredgecolor=‘k’, markersize=10)

plt.show()`

猜你喜欢

转载自blog.csdn.net/seeker3/article/details/108569171
今日推荐