基于Matplotlib按坐标轴设置图表点颜色的技术咨询
问题描述

需要在Matplotlib图表中实现以下两种样式之一:
- 将所有x轴负数值对应的点设为红色,正数值对应的点设为绿色;
- 仅将x轴数值最小的3个点设为红色,最大的3个点设为绿色。
现有代码:
from matplotlib import pyplot as plt ex_x = [res1,res2,res3,res4,res5,res6,res7,res8,res9,res10,res11,res12,res13,res14,res15,res16,res17,res18,res19,res20,res21,res22,res23,res24,res25,res26,res27,res28,res29,res30,res31,res32,res33,res34,res35,res36,res37,res38,res39,res40,res41,res42,res43,res44,res45,res46,res47] ex_y = ["1","2","2.1","2.1.1","2.2","3","3.1","3.2","4","4.1","4.1.1","4.1.1A)","4.1.1B)","4.1.2","4.2","4.2A)","4.2B)","4.3","4.4","5","5.1","5.1.1","5.1.2","5.1.2A)","5.1.2B)","5.1.3","5.1.4","5.2","5.2.1","5.2.1A)","5.2.1B)","5.2.1C)","5.2.2","5.2.3","5.2.3A)","5.2.3B)","5.2.4","5.2.5","6","6.1","7","7.1","7.2","8.1","8.1A)","8.1B)","8.2"] l_x = [0, 0] l_y = [47, 0] z_x = [-10, 10] z_y = [0, 0] plt.grid(True) for x in range(-10,10): plt.plot(l_x, l_y, color = "k", linewidth = 2) plt.plot(z_x, z_y, color = 'k', linewidth = 1) plt.plot(ex_x, ex_y, marker='.') plt.gca().invert_yaxis() plt.title("Perfil Neuropsicológico") plt.tight_layout() plt.show()
解决方案
方案一:按x轴正负值设置颜色
用scatter替代plot,遍历每个数据点,根据x值正负分配对应颜色:
from matplotlib import pyplot as plt # 替换res1-res47为实际数值 ex_x = [res1,res2,res3,res4,res5,res6,res7,res8,res9,res10,res11,res12,res13,res14,res15,res16,res17,res18,res19,res20,res21,res22,res23,res24,res25,res26,res27,res28,res29,res30,res31,res32,res33,res34,res35,res36,res37,res38,res39,res40,res41,res42,res43,res44,res45,res46,res47] ex_y = ["1","2","2.1","2.1.1","2.2","3","3.1","3.2","4","4.1","4.1.1","4.1.1A)","4.1.1B)","4.1.2","4.2","4.2A)","4.2B)","4.3","4.4","5","5.1","5.1.1","5.1.2","5.1.2A)","5.1.2B)","5.1.3","5.1.4","5.2","5.2.1","5.2.1A)","5.2.1B)","5.2.1C)","5.2.2","5.2.3","5.2.3A)","5.2.3B)","5.2.4","5.2.5","6","6.1","7","7.1","7.2","8.1","8.1A)","8.1B)","8.2"] l_x = [0, 0] l_y = [47, 0] z_x = [-10, 10] z_y = [0, 0] plt.grid(True) for x in range(-10,10): plt.plot(l_x, l_y, color = "k", linewidth = 2) plt.plot(z_x, z_y, color = 'k', linewidth = 1) # 按x值正负分配颜色 colors = [] for x_val in ex_x: if x_val < 0: colors.append('red') elif x_val > 0: colors.append('green') else: colors.append('black') # x=0时默认黑色,可自行调整 plt.scatter(ex_x, ex_y, marker='.', color=colors) plt.gca().invert_yaxis() plt.title("Perfil Neuropsicológico") plt.tight_layout() plt.show()
方案二:按x轴极值点设置颜色
先找出x值最小和最大的3个点的索引,再给对应点分配颜色:
from matplotlib import pyplot as plt # 替换res1-res47为实际数值 ex_x = [res1,res2,res3,res4,res5,res6,res7,res8,res9,res10,res11,res12,res13,res14,res15,res16,res17,res18,res19,res20,res21,res22,res23,res24,res25,res26,res27,res28,res29,res30,res31,res32,res33,res34,res35,res36,res37,res38,res39,res40,res41,res42,res43,res44,res45,res46,res47] ex_y = ["1","2","2.1","2.1.1","2.2","3","3.1","3.2","4","4.1","4.1.1","4.1.1A)","4.1.1B)","4.1.2","4.2","4.2A)","4.2B)","4.3","4.4","5","5.1","5.1.1","5.1.2","5.1.2A)","5.1.2B)","5.1.3","5.1.4","5.2","5.2.1","5.2.1A)","5.2.1B)","5.2.1C)","5.2.2","5.2.3","5.2.3A)","5.2.3B)","5.2.4","5.2.5","6","6.1","7","7.1","7.2","8.1","8.1A)","8.1B)","8.2"] l_x = [0, 0] l_y = [47, 0] z_x = [-10, 10] z_y = [0, 0] plt.grid(True) for x in range(-10,10): plt.plot(l_x, l_y, color = "k", linewidth = 2) plt.plot(z_x, z_y, color = 'k', linewidth = 1) # 获取x值排序后的索引 sorted_indices = sorted(range(len(ex_x)), key=lambda i: ex_x[i]) min_3_indices = sorted_indices[:3] # 最小3个点的索引 max_3_indices = sorted_indices[-3:] # 最大3个点的索引 # 分配颜色:默认灰色,极值点设为红/绿色 colors = ['gray'] * len(ex_x) for idx in min_3_indices: colors[idx] = 'red' for idx in max_3_indices: colors[idx] = 'green' plt.scatter(ex_x, ex_y, marker='.', color=colors) plt.gca().invert_yaxis() plt.title("Perfil Neuropsicológico") plt.tight_layout() plt.show()
注意:代码中
res1-res47需替换为实际数值,否则会触发未定义变量错误。
内容的提问来源于stack exchange,提问作者Vinizilin XD
相关产品推荐
相关产品推荐

