我正在处理一个脚本,该脚本根据节点数接收多个输入,解析数据并多次调用绘图函数。
问题是我多次调用绘图函数(请参见下面的代码),但是我不知道如何解决此问题。我看到了this solution,但实际上不是我的案子(或者我不知道如何适用于我的案子)。
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
sns.set(style="whitegrid")
fig, (ax1, ax2, ax3, ax4) = plt.subplots(nrows=1, ncols=4, figsize=(16, 4))
plt.tight_layout()
def plot_data(df, nodes):
global ax1, ax2, ax3, ax4
if nodes == 10:
plt.subplot(141)
ax1 = sns.kdeplot(df['Metric'], cumulative=True, legend=False)
ax1.set_ylabel('ECDF', fontsize = 16)
ax1.set_title('10 Nodes')
elif nodes == 20:
plt.subplot(142)
ax2 = sns.kdeplot(df['Metric'], cumulative=True, legend=False)
plt.setp(ax2.get_yticklabels(), visible=False)
ax2.set_title('20 Nodes')
elif nodes == 30:
plt.subplot(143)
ax3 = sns.kdeplot(df['Metric'], cumulative=True, legend=False)
plt.setp(ax3.get_yticklabels(), visible=False)
ax3.set_title('30 Nodes')
elif nodes == 40:
plt.subplot(144)
ax4 = sns.kdeplot(df['Metric'], cumulative=True, legend=False)
plt.setp(ax4.get_yticklabels(), visible=False)
ax4.set_title('40 Nodes')
df1 = pd.DataFrame({'Metric':np.random.randint(0, 15, 1000)})
df2 = pd.DataFrame({'Metric':np.random.randint(0, 15, 1000)})
df3 = pd.DataFrame({'Metric':np.random.randint(0, 15, 1000)})
nodes = [10, 20, 30, 40]
for i in range(4):
"""
In my real code, the DataFrames are calculated from reading CSV files.
Since that code would be too long, I'm using dummy data.
"""
plot_data(df1, nodes[i])
# I understand that this calls cause the warning,
# but I don't know how to solve it
plot_data(df2, nodes[i])
plot_data(df3, nodes[i])
plt.show()
答案 0 :(得分:0)
我认为,这应该可以满足您的需要-这只是将轴作为参数传递然后将循环放入函数的情况
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
sns.set(style="whitegrid")
fig, axarr = plt.subplots(nrows=1, ncols=4, figsize=(16, 4))
plt.tight_layout()
nodes = [10, 20, 30, 40]
def plot_data(list_of_dfs, axarr, nodes):
for df, ax, node in zip(list_of_dfs, axarr, nodes):
ax = sns.kdeplot(df['Metric'], cumulative=True, legend=False)#I'm not completely sure this needs to be assignment, haven't used seaborn much
ax.set_ylabel('ECDF', fontsize = 16)
ax.set_title('{} Nodes'.format(nodes))
list_of_dfs = [df1, df2, df3]
plot_data(list_of_dfs, axarr, nodes)
plt.show()
答案 1 :(得分:0)
您需要删除plt.subplot(nnn)
。如警告所述,当前执行此操作将重用axis实例。但是在未来的matplotlib版本中,这将创建一个新的axes实例。
解决方案是将已创建的轴作为参数传递给函数,并使用ax=
的{{1}}参数:
seaborn.kdeplot
请注意,您可以通过在sns.set(style="whitegrid")
fig, axes = plt.subplots(nrows=1, ncols=4, figsize=(16, 4))
plt.tight_layout()
def plot_data(df, nodes, axes):
ax1, ax2, ax3, ax4 = axes
if nodes == 10:
sns.kdeplot(df['Metric'], cumulative=True, legend=False, ax=ax1)
ax1.set_ylabel('ECDF', fontsize = 16)
ax1.set_title('10 Nodes')
elif nodes == 20:
sns.kdeplot(df['Metric'], cumulative=True, legend=False, ax=ax2)
plt.setp(ax2.get_yticklabels(), visible=False)
ax2.set_title('20 Nodes')
elif nodes == 30:
sns.kdeplot(df['Metric'], cumulative=True, legend=False, ax=ax3)
plt.setp(ax3.get_yticklabels(), visible=False)
ax3.set_title('30 Nodes')
else:
sns.kdeplot(df['Metric'], cumulative=True, legend=False, ax=ax4)
plt.setp(ax4.get_yticklabels(), visible=False)
ax4.set_title('40 Nodes')
for i in range(4):
plot_data(df1, nodes[i], axes)
plot_data(df2, nodes[i], axes)
plot_data(df3, nodes[i], axes)
plt.show()
中使用sharey=True
并删除fig, axes = plt.subplots(…, sharey=True)