Last active
January 12, 2022 03:22
-
-
Save ugo-nama-kun/d756d1343c2e9c33a5a3bb04dcd959ae to your computer and use it in GitHub Desktop.
よくある matplotlib で plot してそれを保存する方法:heatmap/300 dpi/png
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 seaborn as sns | |
import matplotlib.pyplot as plt | |
# load data | |
x_ticks = np.linspace(0, 1, 10) | |
y_ticks = np.linspace(0, 1, 10) | |
# ちなみに save は np.save("data_matrix.npy", data_matrix) みたいなかんじで | |
data_matrix = np.load("data_matrix.npy") | |
data_sequence = np.load("data_sequence.npy") | |
# setup | |
sns.set() | |
sns.set_context("talk") | |
plt.figure(figsize=(6, 5), dpi=300) | |
PLOT_TYPE = "heat_map" | |
if PLOT_TYPE == "heat_map": | |
# ヒートマップの例 | |
# 参考:https://pod.hatenablog.com/entry/2018/09/20/212527 | |
ax = sns.heatmap(data_matrix, | |
cmap="jet", # Reds, Blues, CMRMap, coolwarm | |
vmin=0., | |
vmax=1., | |
square=True, | |
xticklabels=x_ticks.round(2), | |
yticklabels=y_ticks.round(2)) | |
ax.set_ylabel("red") | |
ax.set_xlabel("blue") | |
ax.invert_yaxis() | |
# color bar の数字をいじりたい時 | |
cbar = ax.collections[0].colorbar | |
cbar.set_ticks([0, .2, .75, 1]) | |
else: | |
# plot + scatter の例 | |
# 参考:https://pythondatascience.plavox.info/matplotlib/%E6%95%A3%E5%B8%83%E5%9B%B3 | |
plt.plot(x_ticks, data_sequence, "b"); | |
plt.scatter(x=x_ticks, y=data_sequence, c="b", s=5, marker="o") | |
# 書き出し | |
plot.figure.savefig('sample.eps', | |
bbox_inches='tight') # 書き出した図が切れなくなる |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
も効果的。
tickを消すには
で全て消す。部分的に消すには
簡単にするには
from: https://stackoverflow.com/questions/12998430/remove-xticks-in-a-matplotlib-plot