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).
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()
A window will appear like this and you can see the graph. You can also save it manually from here.
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
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.
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
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.
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')
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