Tweaking the seaborn graph from pandas dataFrame

python pandas matplotlib seaborn data-analysis

281 просмотра

1 ответ

5742 Репутация автора

I am trying to plot the timeseries graph of internet traffic from 11 cities in Europe.

I got access to the data set from 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()

The graph that I get is shown here.Internet traffic data (bits) in 11 European cities

My questions is mainly about tweaking or beautifying the following graph.

1) I want the date to be marked more frequently on the x-axis. 2) I would prefer the Y-axis numbers to have 0.6x10^12 (or something similar) for each value rather than 1e12 at the very top. 3) I am making calls to matplotlib.pyplot object on a couple of occassions. I would like to avoid it and deal directly with seaborn object

It would be great if someone could help me out.

Автор: liv2hak Источник Размещён: 01.07.2016 02:32

Ответы (1)


0 плюса

342 Репутация автора

Here is how I would do it in two different ways, depending on how you want to format your y-ticks.

I don't have your dataset so I created one myself.

Imports:

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

Code for the first approach:

#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()

Resulting plot:

Plot for first approach

Code for the second approach:

#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()

Resulting plot: Plot for second approach

Автор: Andreq Размещён: 28.07.2016 04:26
Вопросы из категории :
32x32