समय के साथ edge changes की संख्या प्लॉट करें
अब आप कुछ प्लॉट बनाने जा रहे हैं! जिन सूचियों (lists) को आपने पहले बनाया था, वे इस अभ्यास में भी आपके लिए लोड कर दी गई हैं। नीचे दिख रहे कुछ उन्नत matplotlib कोड को लेकर चिंतित न हों: क्या हो रहा है, यह समझाने के लिए कमेंट्स दिए गए हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
इंटरमीडिएट Network Analysis in Python
अभ्यास निर्देश
- समय के साथ जोड़े गए edges की संख्या प्लॉट करें। ऐसा करने के लिए:
- एक list comprehension का उपयोग करके
addedपर iterate करें औरedges_addedनाम की सूची बनाएँ। इस list comprehension का output expressionlen(g.edges())है, जहाँgआपका iterator वैरिएबल है. edges_addedसूची कोax1.plot()में पास करें.
- एक list comprehension का उपयोग करके
- समय के साथ हटाए गए edges की संख्या प्लॉट करें। एक बार फिर list comprehension का उपयोग करें, लेकिन इस बार
addedके बजायremovedपर iterate करें. - fractional changes को समय के साथ प्लॉट करें, इसके लिए उसे
ax2.plot()के आर्ग्युमेंट के रूप में पास करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
# Plot the number of edges added over time
edges_added = [____(____) for ____ in ____]
plot1 = ax1.plot(____, label='added', color='orange')
# Plot the number of edges removed over time
edges_removed = [____(____) for ____ in ____]
plot2 = ax1.plot(____, label='removed', color='purple')
# Set yscale to logarithmic scale
ax1.set_yscale('log')
ax1.legend()
# 2nd axes shares x-axis with 1st axes object
ax2 = ax1.twinx()
# Plot the fractional changes over time
plot3 = ax2.plot(____, label='fractional change', color='green')
# Here, we create a single legend for both plots
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines1 + lines2, labels1 + labels2, loc=0)
plt.axhline(0, color='green', linestyle='--')
plt.show()