python+matplotlib 画一个漂亮的折线统计图
有详细的注释说明……
import matplotlib.pyplot as plt
import numpy as np
import math
def draw_line_chart(Line_data_list,title,pic_name):
'''
:param Line_data_list: 折线数据源
:param title: 图表名称
:param pic_name: 保存图片名称
:return:
'''
# 设置matplotlib的字体参数以支持中文
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置字体为SimHei
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
text_color = '#ffffff' # 文字颜色
canvas_color = '#404040' # 画布背景颜色 #404040
# 绘制折线图
fig=plt.figure(figsize=(19.20, 10.80), facecolor=canvas_color) # 设置画布大小及颜色
x_list=[]
y_list = []
for Line_data in Line_data_list:
plt.plot(Line_data['x_list'], Line_data['y_list'], label=Line_data['name'], color=Line_data['color'],linestyle=None) #values are '-', '--', '-.', ':', 'None', ' ', '', 'solid', 'dashed', 'dashdot', 'dotted'
x_list = Line_data['x_list']
if len(y_list)==0:
y_list = Line_data['y_list']
# 绘制折线图
# plt.plot(x, RQI, label='RQI', color='#4F81BD') # 蓝色
# plt.plot(x, A, label='A', color='#00B050') # 绿色
# plt.plot(x, B, label='B', color='#FFC000') # 橙色
# plt.plot(x, C, label='C', color='#C00000') # 红色
# 修改坐标轴标签的字体颜色
# plt.xlabel('X Axis', color='#BFBFBF')
# plt.ylabel('Y Axis', color='#BFBFBF')
if len(x_list)>100:
step = math.ceil(len(x_list)/100) # 最大值 向上取整
plt.xticks(x_list[::step])
#gradation=[100]
max_value = max(y_list+gradation) #y轴列表中取最大值
max_v=math.ceil(max_value) # 最大值 向上取整
if max_v>20:
step = math.ceil(max_v/20) # 最大值 向上取整
else:
step = 0.5 # 最大值 向上取整
# 设置y轴的刻度间隔
plt.yticks(np.arange(0, max_v, step)) # np.arange(0, max_v, 0.5) 最大值的数列中,0.5取一个值
# x轴刻度描述旋转60度
plt.xticks(rotation=60)
# 修改坐标轴刻度的颜色
plt.xticks(color=text_color)
plt.yticks(color=text_color)
# 修改网格线的颜色和样式
plt.grid(color='#999999', linestyle='--', linewidth=0.5) ##999999
# 尝试修改方格子的背景颜色(这里是一个示例,实际效果可能需要进一步定制)
plt.gca().set_facecolor(canvas_color) # 设置坐标轴背景颜色
# 添加图表元素
plt.title(title, color=text_color, fontsize=20)
# plt.xlabel('X Axis')
# plt.ylabel('Y Axis')
# 创建图例并修改位置
legend = plt.legend(frameon=False, bbox_to_anchor=(0.9, 1.04), ncol=5) # loc='upper center',
for legend_color in legend.get_texts():
legend_color.set_color(text_color)
plt.tight_layout()
# 显示图表
# plt.show()
plt.savefig(pic_name,) #transparent=True,
plt.close(fig)
if __name__ == '__main__':
gradation = [3.6, 3, 2.4] # ABCD等级
data_line_color = '#4F81BD' # 蓝色 画数据图
A_line_color = '#00B050' # 绿色 等级A线
B_line_color = '#FFC000' # 橙色 等级B线
C_line_color = '#C00000' # 红色 等级B线
data_type='Temp'
title='Temp图表'
pic_name='images_01.png'
y_list=[4.32652, 4.45215, 4.62096, 4.64238, 4.27212, 4.44637, 4.29507, 4.4989, 4.56095, 4.57829, 4.68539, 4.65428, 4.71412,
4.57183, 4.63558, 4.65309, 4.67502, 4.43583, 4.57693, 4.63949, 4.70545, 4.66159, 4.57489, 4.61365, 4.60124,
4.63388, 4.62742, 4.65445, 4.6383, 4.63524, 4.66431, 4.05724, 4.5788, 4.69049, 4.72058, 4.59104, 4.56401, 4.55925,
4.6434, 4.18355, 4.30238, 4.65819, 4.59206, 3.57495, 4.17743, 2.13233, 2.00993, 3.73237, 3.26776, 3.71299, 3.96544,
4.71344, 4.72126, 4.67077, 4.68896, 4.70443, 3.98397, 4.55092, 4.66941, 4.7505, 4.76852, 4.59869, 4.63201, 4.72891,
4.58084, 4.70783, 4.69984, 4.70579, 4.69185, 4.74846, 4.75696, 4.708, 4.72381, 4.69406, 4.7386, 4.02715, 4.50893,
4.49941, 4.54089, 4.60515, 4.78926, 4.79572, 4.78943, 4.60719, 4.55432, 4.37973, 4.69763, 4.71293, 4.44824,
4.67247, 4.78824, 4.46507, 4.26056, 4.50961, 4.46609, 4.66584, 4.75067, 4.69933, 4.75475, 4.74999, 4.73027,
4.57472, 4.6128, 4.66193, 4.33077, 4.52355, 3.88367, 4.69729, 4.65122, 4.73571, 4.25155, 3.15148, 3.18055, 3.64346,
4.64884, 4.67842, 4.72585, 4.69899, 4.70817, 4.63762, 4.67638, 4.02681, 4.16536, 4.50383, 4.37208, 4.48904,
4.56877, 4.57455, 4.63949, 4.60243, 4.35202, 4.63235, 4.72211, 4.64935, 4.44076, 3.8104, 3.71537, 4.00216, 3.98329,
4.65513, 4.64306, 4.56673, 4.6077, 4.68182, 4.6043, 4.61025, 4.6723, 4.51318, 4.66142, 4.69049, 4.70545, 4.66414,
4.68063, 4.61178, 4.73265, 4.66805, 4.65139, 4.60498, 4.60175, 4.36341, 4.03735, 4.50689, 4.58611, 4.75968,
4.53256, 4.68029, 4.26617, 4.34828, 4.81595, 4.65802, 4.75305, 4.77107, 4.43362, 4.68318, 4.65513, 4.71429,
4.71191, 4.657, 4.63422, 4.65292, 4.70664, 4.75339, 4.72704, 4.6672, 4.66941, 4.81017, 4.80456, 4.67621, 4.68692,
4.71803, 4.67043, 4.67995, 4.70358, 4.66397, 4.74863, 4.63643, 4.46541, 4.47221, 4.3986, 4.54769, 4.35321, 4.26226,
4.44467, 4.66329, 4.71633, 4.68301, 4.75118, 4.71276, 4.6859, 4.73758, 4.71259, 4.68318, 4.74829, 4.67196, 4.63303,
4.67774, 4.68607, 4.47527, 4.69015, 4.7114, 4.71837, 4.67893, 4.71327, 4.74727, 4.75322, 4.72619, 4.69525, 4.74965,
4.66601, 4.70069, 4.31139, 4.42342, 4.1062, 3.96867, 4.45657, 4.18202, 4.61671, 4.7182, 4.61671, 4.6706, 4.67519,
4.70987, 4.4156, 4.66958, 4.74693, 4.69729, 4.776, 4.72024, 4.64204, 4.72024, 4.69712, 4.76036, 4.71242, 4.51913,
4.50791, 4.6978, 4.35763, 4.56979, 4.36375, 4.66329, 4.75356, 4.79062, 4.75815, 4.60311, 4.64544, 4.60124, 4.40931,
3.8461, 3.8648]
x_list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137,
138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183,
184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206,
207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229,
230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252,
253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269]
Line_data_list=[
{
'x_list':x_list,
'y_list': y_list,
'name': data_type,
'color': data_line_color
},
{
'x_list': x_list,
'y_list': [gradation[0]] * len(y_list),
'name': 'A',
'color': A_line_color
},
{
'x_list': x_list,
'y_list': [gradation[1]] * len(y_list),
'name': 'B',
'color': B_line_color
},
{
'x_list': x_list,
'y_list': [gradation[2]] * len(y_list),
'name': 'C',
'color': C_line_color
}
]
draw_line_chart(Line_data_list,title,pic_name)