【问题标题】:Tweaking the seaborn graph from pandas dataFrame从 pandas dataFrame 调整 seaborn 图
【发布时间】:2016-11-03 07:38:57
【问题描述】:

我正在尝试绘制来自欧洲 11 个城市的互联网流量的时间序列图。

我从internet traffic data of 11 european cities获得了数据集的访问权限

# !/usr/bin/env python3.4
# -*- coding: utf-8 -*-

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_csv('internet-traffic-data-in-bits.csv')

print(df.dtypes)


bp = sns.tsplot([df.Internet_traffic_data_in_bits],color="indianred",)

bp.set(xlabel='Date', ylabel='Internet Traffic Data (bits) in 11 European cities')

plt.xticks(rotation=45)
# plt.tight_layout()
plt.show()

我得到的图表显示在这里。

我的问题主要是关于调整或美化下图。

1) 我希望在 x 轴上更频繁地标记 date。 2) 我希望 Y 轴的数字对于每个值都有 0.6x10^12(或类似的值),而不是最顶部的 1e12。 3) 我在几个场合调用matplotlib.pyplot 对象。我想避开它,直接处理seaborn对象

如果有人可以帮助我,那就太好了。

【问题讨论】:

    标签: python pandas matplotlib seaborn data-analysis


    【解决方案1】:

    以下是我将如何以两种不同的方式执行此操作,具体取决于您希望如何格式化 y-ticks。

    我没有你的数据集,所以我自己创建了一个。

    进口:

    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    # Ticker module
    import matplotlib.ticker as mtick
    

    第一种方法的代码:

    #df = pd.read_csv('internet-traffic-data-in-bits.csv')
    bp = sns.tsplot([df.traffic],color="indianred",)
    bp.set(xlabel='Date', ylabel='Internet Traffic Data (bits) in 11 European cities')
    loc = mtick.MultipleLocator(base=5.0) # This locator puts ticks at regular intervals
    bp.xaxis.set_major_locator(loc) # Apply the locator to the x-axis
    
    # First approach: Format each tick in the "1.52e12" format.
    bp.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2e')) # Format each y-tick in scientific format.
    plt.tight_layout()
    plt.show()
    

    结果图:

    第二种方法的代码:

    #df = pd.read_csv('internet-traffic-data-in-bits.csv')
    bp = sns.tsplot([df.traffic],color="indianred",)
    bp.set(xlabel='Date', ylabel='Internet Traffic Data (bits) in 11 European cities')
    loc = mtick.MultipleLocator(base=5.0) # This locator puts ticks at regular intervals
    bp.xaxis.set_major_locator(loc) # Apply the locator to the x-axis
    
    # Second approach: Fancier formatting using a function.
    def as_si(x, *args):
        """
        Format a number in a custom scientific notation, using a fixed number of decimal places.
        """
        ndp = 2 # Number of decimal places
        s = '{x:0.{ndp:d}e}'.format(x=x, ndp=ndp) # Format the string: 1520 becomes 1.52e3
        m, e = s.split('e') # Split the string around the letter e
        return '{m:s}x10^{e:d}'.format(m=m, e=int(e)) # Return a formatted string with exponent.
    
    bp.yaxis.set_major_formatter(mtick.FuncFormatter(as_si)) # Format each y-tick in the custom format.
    plt.tight_layout()
    plt.show()
    

    结果图:

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 2016-12-01
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 2017-12-09
      • 2019-07-22
      • 2019-01-19
      相关资源
      最近更新 更多