Python combat two: Draw a line chart with data markers using CSV data (matplotlib)

Source: Internet
Author: User

Background:
Automatically obtain bug trend statistics in the defect management system and save to CSV, read the CSV data and draw a line chart with data markers, and save it as a PNG picture

The following code only implements the "read CSV data and draw a line chart with data markers, and save as a PNG picture" feature

#导入需要的模块
import NumPy as NP
import Matplotlib.pyplot as Plt
import Matplotlib.mlab as Mlab
import Matplotlib.ticker as Ticker
#读取CSV数据为numpy record array records
r = Mlab.csv2rec (' d:/python/pj4/data/bug_trend.csv ')
R.sort ()
#形成Y轴坐标数组
N = Len (r)
ind = Np.arange (n)  # The evenly spaced plot indices
#ind1这里是为了把图撑大一点
ind1 = NP. Arange (n+3)
#将X轴格式化为日期形式, the X axis defaults to 0.5 stepping,
#这里将整数X轴坐标格式化为日期,. 5 's not corresponding date,
#因为扩展了3格坐标, so the n+ coordinate point does not show date
def format_date (x, Pos=none):
    if not x%1  and x<n:
        thisind = np.clip (int (x), 0, N-1) return
        R.create_date[thisind]. Strftime ('%y-%m-%d ')
    else: return
        '
#绘图
fig = plt.figure ()
ax = Fig.add_subplot
#下行为了将图扩大一点, hidden by white line shows
ax.plot (Ind1,ind1, '-', color= "White")
#正常要显示的bug总数折线
Ax.plot (Ind, r[' all '], ' o ', label= ' all of BUGs ')
#正常要显示的bug已解决总数折线
Ax.plot (Ind, r[' resolved '], ' o ', label= ' resolved BUGs ')
#正常要显示的bug已关闭总数折线
Ax.plot (Ind, r[' closed '], ' o ', label= ' closed BUGs ')
#图标的标题
ax.set_title (u) BUG Trend Chart ")
#线型示意说明
ax.legend (loc= ' upper left ')

#在折线图上标记数据,-+0.1 is to stagger the point of displaying data
datadotxy= Tuple (Zip (ind-0.1,r[' all "]+0.1))
for Dotxy in Datadotxy:
    ax.annotate (str (int (dotxy[1]-0.1)), Xy=dotxy)

#将X轴格式化为日期形式    
ax.xaxis.set_major_formatter (ticker. Funcformatter (format_date))
fig.autofmt_xdate () 

#显示图片
plt.show ()

#将图片保存到指定目录
Plt.savefig ("D:/python/pj4/img/bug_trend.png")

Effect Chart:

CSV file format diagram:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.