[PYTHON] (For those unfamiliar with Matplotlib) Tips for drawing graphs with Seaborn

This article introduces some small tips that are (probably) not written in existing articles. Although most of them have become the usage of matplotlib, the point that is clogged when a person who has never touched matplotlib suddenly tries to use Seaborn (otherwise, it is explained on the premise of knowledge of matplotlib). I will write that it will be useful in the role of supplementing (where it is).

Display the drawn graph

It is a function on the matplotlib side, and it is displayed when plt.show () is called after drawing.

showyourplot.py


import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns  # cf. https://www.reddit.com/r/learnpython/comments/5oscmr/why_is_seaborn_commonly_imported_as_sns/

Z = np.random.random(1000)

sns.distplot(Z, kde=False)  # kdf stands for Kernel Density Estimation, which is disabled here.
plt.show()

image.png A window will appear like this and you can see the graph. You can also save it manually from here.

Automatically save the drawn graph

This is also saved by the function of matplotlib. Add it to the end of the code above.

plt.savefig("seaborntest.png ")  # save as png file

seaborntest.png A file like this was saved with the filename seaborntest.png.

plt.savefig("seaborntest.pdf")  # output format detected automatically

You can save it as a pdf just by changing the extension. Since the vector data remains, you can modify it later with Adobe Illustrator etc. if necessary.

Prevents multiple graphs from overlapping and being drawn

Even in the above example, it overlaps, but especially when drawing and saving a lot of graphs, it is necessary to erase the contents drawn before each time. To do this, call plt.clf ().

savemanyplots.py


import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

for i in range(10):
    plt.clf()  # flush previous drawings
    sns.set_context('poster')  # use large fonts
    Z = np.random.random(1000)
    sns.distplot(Z, kde=False)
    plt.savefig("seaborntest{0}.png ".format(i),dpi=300)  # output high-resolution png

image.png We were able to generate 10 outputs with different contents. Also, here, the font size is increased with set_context ('poster'), and a high-definition graph is generated with'dpi = 300'with the savefig option.

Play with fonts

Depending on the application, you may want to use italics, superscripts, and subscripts. Below is a sample of iris data tampering with. If you write it in TeX notation like this, it will be reflected as it is. By the way, I erased the x-axis label and changed the y-axis label. We have also changed the color palette.

tweakfonts.py


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

iris = sns.load_dataset('iris')
iris = iris.replace('setosa', '$sp. \mathrm{str.}$')  # italic and roman
iris = iris.replace('versicolor', '$\mathbf{amp^R}$')  # bold with superstring
iris = iris.replace('virginica', '$mutant_1$')  # italic with substring

sns.set_context('poster')
fig = sns.barplot(x='species',y='sepal_length',data=iris,palette='gray')
fig.set_xlabel('')  # no label on the categorical axis
fig.set_ylabel("Survival Rate")  # refine the y-axis label
plt.savefig('survival.png')

survival.png

Link

Some people have already explained how to use Seaborn in general, so please refer to them. http://qiita.com/hik0107/items/3dc541158fceb3156ee0 http://qiita.com/koji_france/items/47fbb89b0922251a20bb http://qiita.com/koji_france/items/87c5d4d08fdc3ad0e9b4 https://nkmk.github.io/blog/python-seaborn-matplotlib/

Recommended Posts

(For those unfamiliar with Matplotlib) Tips for drawing graphs with Seaborn
Drawing tips with matplotlib on the server side
[Python] Customize Colormap when drawing graphs with matplotlib
Real-time drawing with matplotlib
Graph drawing method with matplotlib
Animate multiple graphs with matplotlib
For those who are having trouble drawing graphs in python
[python] How to use the library Matplotlib for drawing graphs
Tips for running Go with docker
Easy to draw graphs with matplotlib
Plot black and white graphs suitable for papers with matplotlib or pylab
Japanese settings for matplotlib and Seaborn axes
Try drawing a normal distribution with matplotlib
Write SVG graphs with matplotlib on heroku
Display Japanese graphs with VS Code + matplotlib
Heat Map for Grid Search with Matplotlib
Tips for using python + caffe with TSUBAME
Tips for plotting multiple lines with pandas
Pandas basics for beginners ③ Histogram creation with matplotlib
Getting Started with Drawing with matplotlib: Writing Simple Functions
Plot ROC Curve for Binary Classification with Matplotlib
[Python] How to draw multiple graphs with Matplotlib
~ Tips for Python beginners from Pythonista with love ① ~
Graph drawing with jupyter (ipython notebook) + matplotlib + vagrant
~ Tips for Python beginners from Pythonista with love ② ~
(Matplotlib) Tips for adjusting the appearance of graphs [Axis / Picture frame / Scale / Scale character edition]