在不重新绘制的情况下重用基本图
Reuse Base Plot Without Replotting
我有一个大数据集,想将整个数据集绘制为背景,然后通过子集化和在背景上绘制来突出显示其中过滤的特征。我通过每次重新绘制背景来完成这项工作,但这非常耗时,因为我基于此渲染了大约 40 个图。
我遇到的问题是我似乎无法将背景数据(第一个散点图)留在原地。通过复制图形或尝试复制轴。
完整功能代码示例:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(
{
"x": np.random.normal(size=100),
"y": np.random.rand(100),
"thing_1": np.concatenate((np.ones(50), np.zeros(50))),
"thing_2": np.concatenate((np.zeros(50), np.ones(50)))}
)
fig, ax = plt.subplots(figsize=(12, 8))
# This works but replots the background data each time (costly with the large datasets)
for thing in ['thing_1', 'thing_2']:
ax.clear()
# background data cloud Reuse instead of plotting
ax.scatter(df.x, df.y, c='grey', alpha=0.5, s=30)
# subset to highlight
ind = df[thing] == 1
ax.scatter(df.loc[ind, 'x'], df.loc[ind, 'y'], c='red', alpha=1, s=15)
plt.savefig('{}_filter.png'.format(thing))
我目前优化代码的最佳尝试:
# Want to do something like this (only plot background data once and copy the axis or figure)
fig_background, ax_background = plt.subplots(figsize=(12, 8))
ax_background.scatter(df.x, df.y, c='grey', alpha=0.5, s=30)
for thing in ['thing_1', 'thing_2']:
fig_filter = fig_background
axs = fig_filter.get_axes()
# subset to highlight
ind = df[thing] == 1
axs[0].scatter(df.loc[ind, 'x'], df.loc[ind, 'y'], c='red', alpha=1, s=15)
plt.savefig('{}_filter.png'.format(thing))
plt.cla()
您可以在绘制新步骤之前移除每个循环步骤中的散点。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(
{
"x": np.random.normal(size=100),
"y": np.random.rand(100),
"thing_1": np.concatenate((np.ones(50), np.zeros(50))),
"thing_2": np.concatenate((np.zeros(50), np.ones(50)))}
)
fig, ax = plt.subplots(figsize=(12, 8))
# background data cloud
ax.scatter(df.x, df.y, c='grey', alpha=0.5, s=30)
scatter = None
for thing in ['thing_1', 'thing_2']:
if scatter is not None:
scatter.remove()
# subset to highlight
ind = df[thing] == 1
scatter = ax.scatter(df.loc[ind, 'x'], df.loc[ind, 'y'], c='red',
alpha=1, s=15)
plt.savefig('{}_filter.png'.format(thing))
我有一个大数据集,想将整个数据集绘制为背景,然后通过子集化和在背景上绘制来突出显示其中过滤的特征。我通过每次重新绘制背景来完成这项工作,但这非常耗时,因为我基于此渲染了大约 40 个图。
我遇到的问题是我似乎无法将背景数据(第一个散点图)留在原地。通过复制图形或尝试复制轴。
完整功能代码示例:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(
{
"x": np.random.normal(size=100),
"y": np.random.rand(100),
"thing_1": np.concatenate((np.ones(50), np.zeros(50))),
"thing_2": np.concatenate((np.zeros(50), np.ones(50)))}
)
fig, ax = plt.subplots(figsize=(12, 8))
# This works but replots the background data each time (costly with the large datasets)
for thing in ['thing_1', 'thing_2']:
ax.clear()
# background data cloud Reuse instead of plotting
ax.scatter(df.x, df.y, c='grey', alpha=0.5, s=30)
# subset to highlight
ind = df[thing] == 1
ax.scatter(df.loc[ind, 'x'], df.loc[ind, 'y'], c='red', alpha=1, s=15)
plt.savefig('{}_filter.png'.format(thing))
我目前优化代码的最佳尝试:
# Want to do something like this (only plot background data once and copy the axis or figure)
fig_background, ax_background = plt.subplots(figsize=(12, 8))
ax_background.scatter(df.x, df.y, c='grey', alpha=0.5, s=30)
for thing in ['thing_1', 'thing_2']:
fig_filter = fig_background
axs = fig_filter.get_axes()
# subset to highlight
ind = df[thing] == 1
axs[0].scatter(df.loc[ind, 'x'], df.loc[ind, 'y'], c='red', alpha=1, s=15)
plt.savefig('{}_filter.png'.format(thing))
plt.cla()
您可以在绘制新步骤之前移除每个循环步骤中的散点。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(
{
"x": np.random.normal(size=100),
"y": np.random.rand(100),
"thing_1": np.concatenate((np.ones(50), np.zeros(50))),
"thing_2": np.concatenate((np.zeros(50), np.ones(50)))}
)
fig, ax = plt.subplots(figsize=(12, 8))
# background data cloud
ax.scatter(df.x, df.y, c='grey', alpha=0.5, s=30)
scatter = None
for thing in ['thing_1', 'thing_2']:
if scatter is not None:
scatter.remove()
# subset to highlight
ind = df[thing] == 1
scatter = ax.scatter(df.loc[ind, 'x'], df.loc[ind, 'y'], c='red',
alpha=1, s=15)
plt.savefig('{}_filter.png'.format(thing))