Skip to content

Instantly share code, notes, and snippets.

@ugo-nama-kun
Last active January 12, 2022 03:22
Show Gist options
  • Save ugo-nama-kun/d756d1343c2e9c33a5a3bb04dcd959ae to your computer and use it in GitHub Desktop.
Save ugo-nama-kun/d756d1343c2e9c33a5a3bb04dcd959ae to your computer and use it in GitHub Desktop.
よくある matplotlib で plot してそれを保存する方法:heatmap/300 dpi/png
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') # 書き出した図が切れなくなる
@ugo-nama-kun
Copy link
Author

plt.axis('tight')

も効果的。
tickを消すには

plt.axis('off')

で全て消す。部分的に消すには

plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom=False,      # ticks along the bottom edge are off
    top=False,         # ticks along the top edge are off
    labelbottom=False) # labels along the bottom edge are off

簡単にするには

plt.xticks([], [])

from: https://stackoverflow.com/questions/12998430/remove-xticks-in-a-matplotlib-plot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment