0

我想创建一个没有 x 或 y 刻度的散点图。但我想看看白格风格的情节。当我明确设置 xticks 时,我也失去了 whitegrids。有什么技巧吗?

import matplotlib.pyplot as plt
plt.style.use('ggplot')
import numpy as np
fig, ax = plt.subplots(frameon=False)

colormap = np.array(["orange","cyan"])
x = np.array([2,2,2,4,4,4,4]*10)
y = np.array([2,4])
col = np.array(['b','g'])
colors = colormap[np.where(y==x[:,None])[1]]
Y = np.random.random((70,2))
plt.xticks([])
plt.yticks([])
ax.scatter(Y[:,0], Y[:,1], c=colors)
4

2 回答 2

2

您要删除刻度标签,而不是刻度本身。最好切换其可见性属性:

plt.setp(ax.get_xticklabels(), visible=False)
plt.setp(ax.get_yticklabels(), visible=False)
于 2021-02-27T15:36:32.430 回答
1

以下是T先生建议的示例,您可以将xticks字体颜色设置为背景颜色。

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

sns.set_style("whitegrid")

fig, ax = plt.subplots(frameon=False)
colormap = np.array(["orange","cyan"])

x = np.array([2,2,2,4,4,4,4]*10)
y = np.array([2,4])

col = np.array(['b','g'])
colors = colormap[np.where(y==x[:,None])[1]]

Y = np.random.random((70,2))

ax.scatter(Y[:,0], Y[:,1], c=colors)
ax.tick_params(axis="both", colors="white") #suggested by Mr T, easier way
# plt.setp(ax.get_xticklabels(),color="white", backgroundcolor="white") # suggested by Me
# plt.setp(ax.get_yticklabels(),color="white", backgroundcolor="white") # suggested by Me

我采用了 T 先生的答案,它更简单、更简单。

输出图像

于 2021-02-27T15:06:02.047 回答