Skip to content

Instantly share code, notes, and snippets.

@sinhrks
sinhrks / df_plot_bar.py
Created March 16, 2014 03:42
Pandas dataframe bar plot sample with flexible bar width and position
import string
from pandas import DataFrame
import numpy as np
from numpy.random import randn
import pandas.tools.plotting as plotting
from matplotlib.pylab import close
@sinhrks
sinhrks / squarify_plot.py
Created March 16, 2014 06:29
Squarify plot example
import squarify
import matplotlib.pyplot as plt
from numpy.random import rand
fig, axes = plt.subplots(2, 3, figsize=(14, 8))
plt.subplots_adjust(top=0.95, bottom=0.05, left=0.05, right=0.95, hspace=0.35)
sq = 8
def random_colors(n):
@sinhrks
sinhrks / df_plot_area.py
Created March 17, 2014 20:23
Pandas dataframe line and area plots
import string
from pandas import DataFrame
import numpy as np
from numpy.random import randn, rand
import pandas.tools.plotting as plotting
from matplotlib.pylab import close
@sinhrks
sinhrks / df_plot_table.py
Created March 18, 2014 14:26
Pandas dataframe with table plotting.
from pandas import Series, DataFrame
import numpy as np
from numpy.random import randn, rand
import pandas.tools.plotting as plotting
import matplotlib.pyplot as plt
s = Series(randn(6), index=range(6))
@sinhrks
sinhrks / df_plot_legend.py
Created March 21, 2014 07:57
Pandas dataframe with legends
from pandas import DataFrame, Series
from numpy.random import randn, rand
df1 = DataFrame(randn(6, 3), index=range(6),
columns=['a', 'b', 'c'])
df2 = DataFrame(randn(6, 3), index=range(6),
columns=['d', 'e', 'f'])
df3 = DataFrame(randn(6, 3), index=range(6),
@sinhrks
sinhrks / df_plot_bar_2.py
Created March 23, 2014 04:57
Pandas dataframe with centered bar
import string
from pandas import DataFrame, Series
from numpy.random import randn
import matplotlib.pyplot as plt
df = DataFrame(randn(7, 4),
index=list(string.ascii_letters[:7]),
columns=['x', 'y', 'z', 'four'])
series = Series(randn(7), index=range(7))
@sinhrks
sinhrks / df_plot_errorbar.py
Created April 8, 2014 13:19
Pandas plotting with errorbars
# -*- coding: utf-8 -*-
import string
from pandas import DataFrame, Series
import datetime
import numpy as np
from numpy.random import randn, rand
import pandas.tools.plotting as plotting
from pandas.compat import u, string_types
@sinhrks
sinhrks / df_plot_pie.py
Created April 27, 2014 00:45
Pandas plotting with pie
import pandas as pd
import numpy as np
import pandas.util.testing as tm
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
series = pd.Series(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], name='series')
@sinhrks
sinhrks / fortify_forecast.R
Created October 4, 2014 05:11
Allow ggplot2 to handle forecast result
library(forecast)
library(ggplot2)
d <- AirPassengers
d.arima <- forecast::auto.arima(d)
d.forecast <- forecast(d.arima, level = c(95), h = 50)
fortify.forecast <- function(forecast.data) {
require(dplyr)
forecasted <- as.data.frame(forecast.data)
@sinhrks
sinhrks / fortify_survfit.R
Created October 4, 2014 10:53
Allow ggplot2 to handle survival::survfit result
library(survival)
library(ggplot2)
library(scales)
d.survfit <- survival::survfit(survival::Surv(time, status) ~ sex,
data = lung)
fortify.survfit <- function(survfit.data) {
data.frame(time = survfit.data$time,