import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#from ggplot import *
import plotly as py
import plotly.graph_objs as go
%matplotlib inline
rc = {'xtick.labelsize': 15, 'ytick.labelsize': 15, 'axes.labelsize': 15, 'font.size': 15,
'legend.fontsize': 15, 'axes.titlesize': 15, "figure.figsize": [14, 6]}
sns.set(rc = rc)
sns.set_style("whitegrid") #to adopt the styling of seaborn
s = pd.Series(np.random.randn(10).cumsum(), index = np.arange(0, 100, 10))
s
0 -0.639765 10 -0.933712 20 -1.135373 30 -0.635447 40 0.206890 50 -0.598782 60 -1.085766 70 -0.665507 80 0.124121 90 -0.664677 dtype: float64
s.plot.line()
fig1 = plt.gcf()
py.offline.iplot_mpl(fig1)
df = pd.DataFrame(np.random.randn(10, 4).cumsum(0),
columns = ["A", "B", "C", "C"],
index = np.arange(0, 100, 10))
df
A | B | C | C | |
---|---|---|---|---|
0 | -1.371746 | 0.393355 | 1.730000 | 0.825433 |
10 | -1.248176 | -0.629889 | 2.346061 | 1.498153 |
20 | -1.265258 | 2.109071 | 3.018069 | 1.343795 |
30 | -1.756939 | -0.418481 | 3.663971 | 0.996378 |
40 | -1.355187 | -1.265352 | 2.544256 | -2.036190 |
50 | -2.551432 | -0.430419 | 1.775690 | -1.953588 |
60 | -1.174741 | 0.139176 | 1.048637 | -3.916210 |
70 | -1.216147 | -0.240921 | -0.812291 | -2.306833 |
80 | -2.741821 | 0.789751 | -1.256852 | -1.386680 |
90 | -5.017580 | 0.113664 | -0.950932 | -0.667759 |
df.plot.line()
fig2 = plt.gcf()
py.offline.iplot_mpl(fig2)
/home/emaasit/anaconda3/lib/python3.6/site-packages/plotly/matplotlylib/renderer.py:384: UserWarning: Bummer! Plotly can currently only draw Line2D objects from matplotlib that are in 'data' coordinates! /home/emaasit/anaconda3/lib/python3.6/site-packages/plotly/matplotlylib/renderer.py:481: UserWarning: I found a path object that I don't think is part of a bar chart. Ignoring.
df = pd.DataFrame(np.random.rand(6, 4),
index=['one', 'two', 'three', 'four', 'five', 'six'],
columns=pd.Index(['A', 'B', 'C', 'D'], name='Genus'))
df
Genus | A | B | C | D |
---|---|---|---|---|
one | 0.554323 | 0.172615 | 0.230385 | 0.293145 |
two | 0.220987 | 0.919625 | 0.702789 | 0.377322 |
three | 0.525143 | 0.605019 | 0.726155 | 0.735294 |
four | 0.010809 | 0.231571 | 0.427825 | 0.321985 |
five | 0.659598 | 0.848622 | 0.447146 | 0.179463 |
six | 0.203172 | 0.595301 | 0.308581 | 0.962861 |
df.plot.bar()
fig3 = plt.gcf()
py.offline.iplot_mpl(fig3)
/home/emaasit/anaconda3/lib/python3.6/site-packages/plotly/matplotlylib/renderer.py:481: UserWarning: I found a path object that I don't think is part of a bar chart. Ignoring.
# stacked bar plots
df.plot.barh(stacked = True, alpha = 0.6);
# tips is one of the datasets that comes with seaborn
tips = sns.load_dataset("tips")
tips.head()
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
sns.barplot(x = "total_bill", y = "day", data = tips);
sns.barplot(data = tips, x = "total_bill", y = "day", hue = "time", alpha = .6);
p = sns.distplot(tips["total_bill"])
p.set(xlabel = "Total bill", ylabel = "Density", title = "Distribution of Total bills")
fig6 = plt.gcf();
py.offline.iplot_mpl(fig6)
sns.jointplot(x = "total_bill", y = "tip", data = tips, kind = "reg")
fig7 = plt.gcf()
py.offline.iplot_mpl(fig7)
/home/emaasit/anaconda3/lib/python3.6/site-packages/plotly/matplotlylib/renderer.py:445: UserWarning: Dang! That path collection is out of this world. I totally don't know what to do with it yet! Plotly can only import path collections linked to 'data' coordinates /home/emaasit/anaconda3/lib/python3.6/site-packages/plotly/matplotlylib/renderer.py:481: UserWarning: I found a path object that I don't think is part of a bar chart. Ignoring.
#### Saving plots for papers
rs = np.random.RandomState(10)
d = rs.normal(size=100)
fig, ax = plt.subplots()
sns.distplot(d, kde=False, color="b")
ax.set_xlabel("Temperature")
ax.set_ylabel("Probability")
plt.savefig('distplot.png', dpi=400, bbox_inches='tight')
ggplot(mtcars, aes('wt', 'mpg')) + \
geom_point() + \
theme_bw()
<ggplot: (8787870004393)>
ggplot(diamonds, aes(x='carat', y='price', color='color')) +\
geom_point(alpha=0.1) + theme_bw()
<ggplot: (8787869928049)>
ggplot(diamonds, aes(x='carat', y='price', color='cut')) +\
geom_point() +\
scale_color_brewer(type='diverging', palette=4) +\
xlab("Carats") + ylab("Price") + ggtitle("Diamonds") + \
theme_bw()
<ggplot: (-9223363248986587760)>
import rpy2.robjects.lib.ggplot2 as gg
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
base = importr("base")
utils = importr("utils")
mtcars = ro.r.data(datasets).fetch("mtcars")["mtcars"]
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-25-d0e28570565c> in <module>() 5 base = importr("base") 6 utils = importr("utils") ----> 7 mtcars = ro.r.data(datasets).fetch("mtcars")["mtcars"] NameError: name 'datasets' is not defined
trace0 = go.Scatter(
x=[1, 2, 3, 4],
y=[10, 15, 13, 17]
)
trace1 = go.Scatter(
x=[1, 2, 3, 4],
y=[16, 5, 11, 9]
)
data = [trace0, trace1]
py.offline.iplot(data)
trace3 = go.Heatmap(z=[[1, 20, 30],
[20, 1, 60],
[30, 60, 1]])
data = Data([trace3])
py.offline.iplot(data)
# Create random data with numpy
import numpy as np
N = 1000
random_x = np.random.randn(N)
random_y = np.random.randn(N)
# Create a trace
trace = go.Scatter(
x = random_x,
y = random_y,
mode = 'markers'
)
data = [trace]
# Plot and embed in ipython notebook!
py.offline.iplot(data, filename='basic-scatter')
# Create random data with numpy
import numpy as np
N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N)+5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N)-5
# Create traces
trace0 = go.Scatter(
x = random_x,
y = random_y0,
mode = 'markers',
name = 'markers'
)
trace1 = go.Scatter(
x = random_x,
y = random_y1,
mode = 'lines+markers',
name = 'lines+markers'
)
trace2 = go.Scatter(
x = random_x,
y = random_y2,
mode = 'lines',
name = 'lines'
)
data = [trace0, trace1, trace2]
py.offline.iplot(data, filename='scatter-mode')
import numpy as np
trace1 = go.Scatter(
y = np.random.randn(500),
mode='markers',
marker=dict(
size='16',
color = np.random.randn(500), #set color equal to a variable
colorscale='Viridis',
showscale=True
)
)
data = [trace1]
py.offline.iplot(data, filename='scatter-plot-with-colorscale')