zoukankan      html  css  js  c++  java
  • matplotlib 库的使用

    1.问题描述:

    在学习kaggle经典学习项目Titanic,进行数据可视化处理时,对于每个特征进行相关性分析(也就是绘制pearson correlation heatmap )热力相关性矩阵时, plt.show() 图形绘制出来,字体会重叠.导致无法观察

    # Visualisations
    """将数据进行可视化"""
    print(train.head(3))
    # correlation heatmap   相关性热点矩阵
    
    plt.figure(figsize=(14,12))
    plt.title('Pearson Correlation of Features',y=1.05,size=15)
    sns.heatmap(train.astype(float).corr(),linewidths=0.1,vmax=1.0,
                square=True,linecolor='white',annot=True)
    plt.show()
    

    导致看不清坐标轴每个特征的含义

    2.解决问题:

    思路:只要找到plt所含有的命令,将X.Y坐标轴字体进行旋转即可

    寻找python xlabel 的文档

    Help on function xlabel in module matplotlib.pyplot:
    
    xlabel(s, *args, **kwargs)
        Set the *x* axis label of the current axis.
        
        Default override is::
        
          override = {
              'fontsize'            : 'small',
              'verticalalignment'   : 'top',
              'horizontalalignment' : 'center'
              }
        
        .. seealso::
        
            :func:`~matplotlib.pyplot.text`
                For information on how override and the optional args work
    

    发现没有关于xlabel进行旋转的参数,

    寻找 python xticks 文档

    Help on function xticks in module matplotlib.pyplot:
    
    xticks(*args, **kwargs)
        Get or set the *x*-limits of the current tick locations and labels.
        
        ::
        
          # return locs, labels where locs is an array of tick locations and
          # labels is an array of tick labels.
          locs, labels = xticks()
        
          # set the locations of the xticks
          xticks( arange(6) )
        
          # set the locations and labels of the xticks
          xticks( arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') )
        
        The keyword args, if any, are :class:`~matplotlib.text.Text`
        properties. For example, to rotate long labels::
        
          xticks( arange(12), calendar.month_name[1:13], rotation=17 )
    

    上面黑体下划线,rotate long label

    找到参数 rotation

    plt.figure(figsize=(14,12))
    plt.title('Pearson Correlation of Features',y=1.05,size=15)
    sns.heatmap(train.astype(float).corr(),linewidths=0.1,vmax=1.0,
                square=True,linecolor='white',annot=True)
    plt.xticks(rotation=90)    # 将字体进行旋转
    plt.yticks(rotation=360)
    plt.show()
    

    标签旋转,可清晰看到每个特征的含义

    问题思考:

    就是plt.show() 输出不了中文字体,这个问题还没有解决

    不要用狭隘的眼光看待不了解的事物,自己没有涉及到的领域不要急于否定. 每天学习一点,努力过好平凡的生活.
  • 相关阅读:
    Vue(小案例_vue+axios仿手机app)_go实现退回上一个路由
    nyoj 635 Oh, my goddess
    nyoj 587 blockhouses
    nyoj 483 Nightmare
    nyoj 592 spiral grid
    nyoj 927 The partial sum problem
    nyoj 523 亡命逃窜
    nyoj 929 密码宝盒
    nyoj 999 师傅又被妖怪抓走了
    nyoj 293 Sticks
  • 原文地址:https://www.cnblogs.com/GeekDanny/p/9220720.html
Copyright © 2011-2022 走看看