Last active
January 14, 2022 15:55
-
-
Save rameshKrSah/70ded1b08f0c662491ff271935e5c2f0 to your computer and use it in GitHub Desktop.
Configuration for Seaborn and matplotlib for plotting graphs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
# this is the setting for plots for research paper and articles. | |
%matplotlib inline | |
%config InlineBackend.figure_format = 'retina' | |
# Set the global font to be DejaVu Sans, size 10 (or any other sans-serif font of your choice!) | |
plt.rc('font',**{'family':'sans-serif','sans-serif':['DejaVu Sans'],'size':10}) | |
# Set the font used for MathJax - more on this later | |
plt.rc('mathtext',**{'default':'regular'}) | |
# Set the style for seaborn | |
plt.style.use(['seaborn-whitegrid', 'seaborn-paper']) | |
import matplotlib.pylab as pylab | |
params = {'legend.fontsize': 'small', | |
'axes.labelsize': 'medium', | |
'axes.titlesize': 'medium', | |
'xtick.labelsize': 'small', | |
'ytick.labelsize': 'small' | |
} | |
pylab.rcParams.update(**params) | |
import seaborn as sns | |
sns.set_context(rc=params) | |
def stylize_axes(ax, title): | |
""" | |
Stylize the axes by removing ths spines and ticks. | |
""" | |
# removes the top and right lines from the plot rectangle | |
ax.spines['top'].set_visible(False) | |
ax.spines['right'].set_visible(False) | |
ax.xaxis.set_tick_params(top=False, direction='out', width=1) | |
ax.yaxis.set_tick_params(right=False, direction='out', width=1) | |
# Enforce the size of the title, label and tick labels | |
ax.set_xlabel(ax.get_xlabel(), fontsize='large') | |
ax.set_ylabel(ax.get_ylabel(), fontsize='large') | |
ax.set_yticklabels(ax.get_yticklabels(), fontsize='medium') | |
ax.set_xticklabels(ax.get_xticklabels(), fontsize='medium') | |
ax.set_title(title, fontsize='large') | |
def save_image(fig, title): | |
""" | |
Save the figure as PNG and pdf files | |
""" | |
if title is not None: | |
fig.savefig(title+".png", dpi=300, bbox_inches='tight', transparent=True) | |
fig.savefig(title+".pdf", bbox_inches='tight') | |
def figure_size(fig, size): | |
fig.set_size_inches(size) | |
fig.tight_layout() | |
def resadjust(ax, xres=None, yres=None): | |
""" | |
Send in an axis and fix the resolution as desired. | |
""" | |
if xres: | |
start, stop = ax.get_xlim() | |
ticks = np.arange(start, stop + xres, xres) | |
ax.set_xticks(ticks) | |
if yres: | |
start, stop = ax.get_ylim() | |
ticks = np.arange(start, stop + yres, yres) | |
ax.set_yticks(ticks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
made public