许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  数据可视化:美化你的图表(4)Styling your plots

数据可视化:美化你的图表(4)Styling your plots

阅读数 4
点赞 0
article_banner

Styling your plots

Introduction

我们需要明白一点,在向读者展现图形的时候,图形的格式很重要。修改图形的格式,使新的style更加漂亮,

   将很大程度上提高图形的交互性,使工作更加有效。

   这一节中,我们要学习的怎么在已经作出的图中,修改格式。一般而言,你可以在你的plot中做任何事情,修改

   任何的格式,我们并不会介绍所有的style,只是介绍最基础的图形格式:改变图形大小,颜色, 字体  ,添加标题,

   去掉坐标线。

   每一个工具都有自己特有的API函数,来修改不同的style,例如matplotlib和seaborn等都要去查对应的API。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline
df_reviews = pd.read_csv('../dataSet/winemag-data_first150k.csv',index_col=0)

df_reviews.head(3)

countrydescriptiondesignationpointspriceprovinceregion_1region_2varietywinery
0USThis tremendous 100% varietal wine hails from …Martha’s Vineyard96235.0CaliforniaNapa ValleyNapaCabernet SauvignonHeitz
1SpainRipe aromas of fig, blackberry and cassis are …Carodorum Selección Especial Reserva96110.0Northern SpainToroNaNTinta de ToroBodega Carmen Rodríguez
2USMac Watson honors the memory of a wine once ma…Special Selected Late Harvest9690.0CaliforniaKnights ValleySonomaSauvignon BlancMacauley

Points on style(在特征points上的格式)

继续使用我们前面介绍过的bar plot。

df_reviews['points'].value_counts().sort_index().plot.bar()
<matplotlib.axes._subplots.AxesSubplot at 0x7f15674e0d68>

这里写图片描述

虽然这个图片,看起来还不错。但是如果你想修改它的大小,更清晰的看清细节,我们可以使用参数figsize来实现。

df_reviews['points'].value_counts().sort_index().plot.bar(figsize=(12,6))
<matplotlib.axes._subplots.AxesSubplot at 0x7f156d7e1f28>

这里写图片描述

figsize控制图片的大小,它的单位是英寸。使用的参数对应的是(宽,高).接下来,我们将改变颜色,color参数。

df_reviews['points'].value_counts().sort_index().plot.bar(
    figsize=(12,6),
    color='red'
)
<matplotlib.axes._subplots.AxesSubplot at 0x7f156d6e7550>

这里写图片描述

当图片非常小的时候,坐标轴上的文本,我们很难看清楚,这里的图像很大,并不会有什么影响。其实,可以通过fontsize参数来修改文本的大小。

df_reviews['points'].value_counts().sort_index().plot.bar(
    figsize=(12,6),
    color='red',
    fontsize=16
)
<matplotlib.axes._subplots.AxesSubplot at 0x7f156d516048>

这里写图片描述

我们还需要一个标题。需要提醒大家的是:matplotlib并不是很好的支持 中文 的显示。

df_reviews['points'].value_counts().sort_index().plot.bar(
    figsize=(12,6),
    color='red',
    fontsize=16,
    title='Ranking Given by Wine Magazin',
)
<matplotlib.axes._subplots.AxesSubplot at 0x7f156c9f4080>

这里写图片描述

很不幸,我们的标题有点小,然而pandas的数据可视化,并没有给我们提过修改标题大小的参数。但是我们知道pandas的数据可视化接口都来自matlotlib库,而且matplotlib提供的修改title的大小的方法。因此我们可以先得到matpoltlib对象,再进行添加其他的参数。

ax = df_reviews['points'].value_counts().sort_index().plot.bar(
    figsize=(12,6),
    color='red',
    fontsize=16
)

ax.set_title('Ranking Given by Wine Magazin',fontsize=20)
<matplotlib.text.Text at 0x7f156aa13f98>

这里写图片描述

在上个 cell  中,我们首先得到matplotlib的对应,在进行设置其他的参数。下面要介绍另一个包seaborn,它比matplotlib更简单。

   它也是基于matplotlib进行封装的。我们可以使用seaborn.despine方法,去掉坐标轴的线。

import seaborn as sns

ax = df_reviews['points'].value_counts().sort_index().plot.bar(
    figsize=(12,6),
    color='red',
    fontsize=16,
)
ax.set_title('Ranking Given by Wine Magazin', fontsize=20)

sns.despine(bottom=True, left=True)

这里写图片描述

Prefect,上面这个图已经很清晰的解释了图表的意义。对图片的style,我们能够做的事情有很多,不仅仅是上面介绍的一些。不同的plot有不同style格式,color是最普遍的,在scatter中s对应的是size。

练习

提高设计的技巧。

df_pokemon = pd.read_csv('../dataSet/pokemon_simple.csv')
df_pokemon.head()

#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
12IvysaurGrassPoison4056062638080601False
23VenusaurGrassPoison525808283100100801False
33VenusaurMega VenusaurGrassPoison62580100123122120801False
44CharmanderFireNaN3093952436050651False
# 画出 Attacl 和 Defense对应的散点图,并修改大小,添加title
df_pokemon.plot.scatter(x='Attack', y='Defense', figsize=(12,6), title='Pokemon by Attack and Defense')
<matplotlib.axes._subplots.AxesSubplot at 0x7f156a93b6a0>

这里写图片描述

# 显示Total变量对应的直方图分布
ax = df_pokemon['Total'].plot.hist(
    figsize=(10,5),
    fontsize=14,
    color='gray',
)
ax.set_title('Pokemon by Total.', fontsize=20)
<matplotlib.text.Text at 0x7f155d3c2e80>

这里写图片描述

# pokemon Type对应的主要类型
ax = df_pokemon['Type 1'].value_counts().plot.bar(
    figsize=(12,6),
    color='green',
    fontsize=14,
)
ax.set_title('Pokemon by Primary Type', fontsize=20)
sns.despine(bottom=True,left=True)

这里写图片描述

总结

在这个section中,我们学习了一些小的作图的技巧,使我们的图形更加的吸引人,高效,便于交互。这里主要学习了matplotlib库函数。下一节我们要学习的Subplots。

原文地址: https://www.kaggle.com/residentmario/bivariate-plotting-with-pandas




免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删


相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空