Python-BUG数据可视化 (数据处理后的视觉呈现优化)

  接着上一个练习,在整合了BUG数据之后,表格的表达方式,还是不够直观,一目了然。所以我就想着如果把数据直接以直方图或者是折线图的方式呈现,会更直观些。

这里主要用到的是一个  matplotlib 的第三方库  (可通过pip3 install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple  下载 (window) )

:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.bar.html

:https://matplotlib.org/stable/gallery/lines_bars_and_markers/barchart.html#sphx-glr-gallery-lines-bars-and-markers-barchart-py

# 直方图

df.plot.bar(x=None,y=None,**kwargs)

X & Y:标签或位置 - 允许回执一列与另一列的关系图。如果未指定,则使用DataFrame的索引 (Y 未指定,则使用所有数字列)
plt.text(x - 0.1, y + 0.05, '%1.0f' % y, ha='center', va='bottom')

x,y表示标签添加的位置,默认是根据坐标轴的数据来度量的,是绝对值,也就是说图中点所在位置的对应的值
'%1.0f' % y   标签的符号

va = 表示垂直对齐方式 ,可选 ‘center’ ,‘top’ , ‘bottom’,‘baseline’ 等
ha = 表示水平对齐方式 ,可以填 ‘center’ , ‘right’ ,‘left’ 等
 1     def zhifangtu_shuangzhu(self):
 2         df = pd.read_excel("Report_data.xlsx", 'Sheet2')
 3         # 取列值
 4         y_list1 = (df.iloc[:, 1]).values
 5         y_list2 = (df.iloc[:, 2]).values
 6 
 7         #绘图
 8         df.plot.bar(x='迭代', y=['版本内', '线上'],width=0.35)
 9 
10         # 定制标题,y轴标题
11         plt.title('版本内BUG与线上BUG对比图')
12         plt.ylabel('BUG数量/个')
13 
14         # 为每个条形图添加数值标签
15         for x, y in enumerate(y_list1):
16             plt.text(x - 0.1, y + 0.05, '%1.0f' % y, ha='center', va='bottom')
17         for x, y in enumerate(y_list2):
18             plt.text(x + 0.1, y + 0.05, '%1.0f' % y, ha='center', va='bottom')
19         plt.show()
直方图

 

运行后结果:

 

 

 

#折线图

plt.xticks(x,x_list, fontsize=15) 
xticks()函数原型 :xticks(ticks, [labels], **kwargs)
ticks :数组类型,用于设置X轴刻度之间间隔
[lables] :数组类型,用于设置每个间隔的显示标签
**kwargs :用于设置标签字体倾斜服和颜色外观属性 (这里可以接收多个关键字)
x = np.arange(len(x_list))
图中将X的刻度间隔根据X数组长度设置为【0-14】,每个刻度的标签为X的对应下标的取值

    def zhexiantu_02(self):
        df = pd.read_excel("Report_data.xlsx", 'Sheet2')
        # plot()绘制折线图
        df.plot(x='迭代', y=['版本内', '线上'])

        plt.title('版本内BUG与线上BUG对比图')
        plt.ylabel('BUG数量/个')
        # 取列值
        x_list = (df.iloc[:, 0]).values
        y_list1 = (df.iloc[:, 1]).values
        y_list2 = (df.iloc[:, 2]).values
        x = np.arange(len(x_list))
        plt.xticks(x,x_list, fontsize=15)
        # 为每个条形图添加数值标签
        for x, y in enumerate(y_list1):
            plt.text(x, y + 0.1, '%1.0f' % y, ha='center',va='bottom')
        for x, y in enumerate(y_list2):
            plt.text(x, y - 0.1, '%1.0f' % y, ha='center',va='bottom')

        plt.show()
折线图

运行后结果:

 

 

 


posted @ 2021-03-26 15:20  依米花开了  阅读(221)  评论(0编辑  收藏  举报