Recently, we need to draw function diagrams for some data, because the requirements are not very high. I hope to draw a diagram as soon as possible, and I don't want to install any special software. I just use the python drawing package matplotlib to do it, which is convenient and fast, and the drawing is not bad. Here is a brief record.
There are two steps to draw a graph: 1. Prepare data; 2. Draw and display. Here's the code. It's easy to see the function of each part
#!/usr/bin/env python # coding=utf-8 import platform import matplotlib from matplotlib.font_manager import * sysstr = platform.system() if(sysstr == "Windows"): # Chinese character processing in windows myfont = FontProperties(fname='C:\Windows\Fonts\simhei.ttf') #Choose the appropriate font file here else: # Chinese character processing under linux import sys reload(sys) sys.setdefaultencoding('utf-8') matplotlib.use('Agg') #If you do not install x11 on the server, you can output the picture and save it, otherwise you may report an error myfont = FontProperties(fname='/usr/share/fonts/truetype/wqy/wqy-microhei.ttc') #Choose the appropriate font file here matplotlib.rcParams['axes.unicode_minus'] = False import matplotlib.pyplot as plt import numpy as np # Define the variables used timebar = [] absolute = [] basevalue = 1000000.0 index = [] relativeMax = [] period = 30 idx = 0 #Read variables in from the file and put them in the appropriate variables for line in open("pnl.txt",'r'): row = line.split('\t') timebar.append(int(row[0])) value = float(row[2])/basevalue absolute.append(value) if (idx % period == 0): #The value is counted periodically here index.append(idx) relativeMax.append(basevalue-min(absolute)) idx+=1 #Drawing fig1 = plt.figure() plt.title("curve",fontproperties=myfont) #Name of the entire coordinate chart plt.xlabel('timebar') #x axis name plt.ylabel('absolute') #y axis name plt.plot(timebar,absolute) fig1.savefig("curve.png") #Save pictures plt.show() #display picture #Write periodic statistics to file with open("summary.txt",'w') as summaryFile: i = 0 for idx in index: output = "{0},{1:.2%},{2:.2%}\n".format(i,absolute[idx],relativeMax[i]) summaryFile.write(output) i+=1
The pnl.txt file is as follows:
1524096000 10000.154785 1010000.154785 1524182400 2988.606417 1012988.761203 1524268800 -3999.066491 1008989.694711 1524355200 -2212.860282 1006776.834429 ... ... ...