Python用matplotlib库制作散点图,有三列数据,如下所示,前两列数据分别作X轴、Y轴,剩余一列数据通过点的颜色渐变来表示不同的数据,在一开始,我将第三列数据(以列表形式)传递给scatte()的形参c引发报错,所以我也不知道该怎样办了。
这是我现在的代码:
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
import matplotlib.pyplot as mp
import csv
import Functions
font = FontProperties(fname=r"c:\\windows\\fonts\\simsun.ttc")
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
filename = '..\\节气与舌象.csv'
x_v = []
y_v = []
color_v = []
with open(filename, encoding='gbk') as file:
reader = csv.reader(file)
for row in reader:
x_v.append(row[0])
y_v.append(row[1])
color_v.append(row[2])
color = Functions.transform_list_to_numpy(color_v)
# mp.style.use('seaborn') 发生警告,并且错误显示
fig, ax = mp.subplots()
ax.scatter(x_v, y_v, c=color, cmap=mp.cm.Blues, s=10) # c=color, cmap=mp.cm.Blues
ax.set_title('节气与舌象', fontsize=15, fontproperties=font)
ax.set_ylabel('舌象', fontsize=1, fontproperties=font)
ax.set_xlabel('节气', fontsize=10, fontproperties=font)
ax.tick_params(axis='both', which='major', labelsize=5)
mp.show()
# mp.savefig('节气与舌象.png', bbox_inches='tight')'
x_v = []
y_v = []
color_v = []
with open(filename, encoding='gbk') as file:
reader = csv.reader(file)
for row in reader:
x_v.append(row[0])
y_v.append(row[1])
color_v.append(row[2])
color = Functions.transform_list_to_numpy(color_v)
# mp.style.use('seaborn') 发生警告,并且错误显示
fig, ax = mp.subplots()
ax.scatter(x_v, y_v, c=color, cmap=mp.cm.Blues, s=10) # c=color, cmap=mp.cm.Blues
ax.set_title('节气与舌象', fontsize=15, fontproperties=font)
ax.set_ylabel('舌象', fontsize=1, fontproperties=font)
ax.set_xlabel('节气', fontsize=10, fontproperties=font)
ax.tick_params(axis='both', which='major', labelsize=5)
mp.show()
# mp.savefig('节气与舌象.png', bbox_inches='tight')
相关文件:Functions.py
import numpy as np
def transform_list_to_numpy(list_val: list):
matrix = np.ndarray(list_val)
return matrix
注:图片中的文件只是数据样式示例
请各位指点!