Skip to content

Instantly share code, notes, and snippets.

@jmquintana79
Last active July 29, 2020 11:12
Show Gist options
  • Save jmquintana79/8645847d8b955cd1f8bb83ff860ecb9f to your computer and use it in GitHub Desktop.
Save jmquintana79/8645847d8b955cd1f8bb83ff860ecb9f to your computer and use it in GitHub Desktop.
Plot with pandas df quickly
# Homepage: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html
import pandas as pd
import matplotlib.pyplot as plt
# plot
DF.plot(x='year', y=['var1', 'var2'],kind="line",subplots=False,layout=(2,2))
# layout: distribution of multicharts.
# subplots: plot each line in different charts.
## customize datetime xticks
xticks = pd.date_range(start=df.index.tolist()[0], end=df.index.tolist()[-1])
df.plot(ax=ax,xticks=xticks.to_pydatetime())
ax.set_xticklabels([x.strftime('%Y %b %d,%a ') for x in xticks],rotation='vertical',fontsize='small')
# pandas plot linked to defined axes
fig, ax = plt.subplots()
DF.plot(ax=ax)
## scatter give custom points (size, color) -> 4D
df.plot(kind="scatter", x="var1", y="var2", alpha=0.4,
s=df["var3"]/100, label="var3",
c="var4", cmap=plt.get_cmap("jet"), colorbar=True,
)
plt.legend()
# SCATTER MATRIX
from pandas.tools.plotting import scatter_matrix
scatter_matrix(DF)
"""
kind : str
‘line’ : line plot (default)
‘bar’ : vertical bar plot
‘barh’ : horizontal bar plot
‘hist’ : histogram
‘box’ : boxplot
‘kde’ : Kernel Density Estimation plot
‘density’ : same as ‘kde’
‘area’ : area plot
‘pie’ : pie plot
‘scatter’ : scatter plot
‘hexbin’ : hexbin plot
"""
# display
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment