I'm working on a script that receives several inputs, parses the data and calls a plotting function several times, according to the number of nodes.
The issue is that I call my plotting function multiple times (see code below), but I don't know how to solve this issue. I saw this solution, but it's not really my case (or I don't know how to apply to my case).
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()