赞
踩
下面这四种配色是不需要指定的,Python自带的主题,无论有多少个种类都适合,这里就简单以条形图为例。
import matplotlib.pyplot as plt data = { "apple": 2.03, "bob": 1.96, "cel": 1.34, "daddy": 1.33, "egg": 1.23, "flow": 1, "glow": 0.99, "hight": 0.82, "illnes": 0.78, "joker": 0.48, "kill": 0.21, "low": 0.15, "mammy": 0.13 } # 将字典按值排序 sorted_data = sorted(data.items(), key=lambda x: x[1]) # 提取标签和值 labels = [item[0] for item in sorted_data] values = [item[1] for item in sorted_data] # 设置图形大小和字体大小 plt.rcParams['figure.figsize'] = (14, 10) plt.rcParams['font.size'] = 16 # 为每个条形图分配不同的颜色 colors = plt.cm.plasma(np.linspace(0, 1, len(labels))) plt.barh(labels, values, color=colors) plt.xlabel('title') # plt.title('各部门/单位数量') # 保存图片 plt.savefig('1.png', bbox_inches='tight') # 显示条形图 plt.show()
核心代码是下面这句话:
colors = plt.cm.plasma(np.linspace(0, 1, len(labels)))
import matplotlib.pyplot as plt data = { "apple": 2.03, "bob": 1.96, "cel": 1.34, "daddy": 1.33, "egg": 1.23, "flow": 1, "glow": 0.99, "hight": 0.82, "illnes": 0.78, "joker": 0.48, "kill": 0.21, "low": 0.15, "mammy": 0.13 } # 将字典按值排序 sorted_data = sorted(data.items(), key=lambda x: x[1]) # 提取标签和值 labels = [item[0] for item in sorted_data] values = [item[1] for item in sorted_data] # 设置图形大小和字体大小 plt.rcParams['figure.figsize'] = (14, 10) plt.rcParams['font.size'] = 16 # 为每个条形图分配不同的颜色 colors = plt.cm.inferno(np.linspace(0, 1, len(labels))) plt.barh(labels, values, color=colors) plt.xlabel('title') # plt.title('各部门/单位数量') # 保存图片 plt.savefig('1.png', bbox_inches='tight') # 显示条形图 plt.show()
核心代码是下面这句话:
colors = plt.cm.inferno(np.linspace(0, 1, len(labels)))
import matplotlib.pyplot as plt data = { "apple": 2.03, "bob": 1.96, "cel": 1.34, "daddy": 1.33, "egg": 1.23, "flow": 1, "glow": 0.99, "hight": 0.82, "illnes": 0.78, "joker": 0.48, "kill": 0.21, "low": 0.15, "mammy": 0.13 } # 将字典按值排序 sorted_data = sorted(data.items(), key=lambda x: x[1]) # 提取标签和值 labels = [item[0] for item in sorted_data] values = [item[1] for item in sorted_data] # 设置图形大小和字体大小 plt.rcParams['figure.figsize'] = (14, 10) plt.rcParams['font.size'] = 16 # 为每个条形图分配不同的颜色 colors = plt.cm.cividis(np.linspace(0, 1, len(labels))) plt.barh(labels, values, color=colors) plt.xlabel('title') # plt.title('各部门/单位数量') # 保存图片 plt.savefig('1.png', bbox_inches='tight') # 显示条形图 plt.show()
核心代码是下面这句话:
colors = plt.cm.cividis(np.linspace(0, 1, len(labels)))
colors = plt.cm.viridis(np.linspace(0, 1, len(labels)))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。