Matplotlib散点图中1:1关系对角线未显示,求排查原因
Hey there, let's break down why your 1:1 comparison line isn't appearing in your plot. You're trying to add a diagonal line to contrast your luminosity data with Drout's, but the line is nowhere to be seen—here's the root of the problem and how to fix it:
Your Original Code
plot23 = sns.regplot(x = Combined['log10(L/L_solar)'], y = Combined['logLum'], fit_reg=False).set_title('Figure 23: Comparing luminosities') plt.xlabel('logL from my data', fontsize=13) plt.ylabel('logL from Drout', fontsize=13) plt.axis((4, 6, 4, 6)) plt.plot([0,0], [6,6], 'k-') plt.show() plot23.figure.savefig("figure23.png")
What's Going Wrong?
Take a close look at your plt.plot() call: plt.plot([0,0], [6,6], 'k-') draws a vertical line at x=0 spanning from y=0 to y=6. But you've set your plot's axis limits to (4, 6, 4, 6)—this means your x-axis only displays values between 4 and 6. That vertical line at x=0 is completely outside the visible range of your plot, so it never shows up!
Fix: Draw the Proper 1:1 Line
A 1:1 relationship means y equals x across your axis range. To match your plot's visible limits, you need to draw a line where both x and y go from 4 to 6. Update your plt.plot() line to:
plt.plot([4, 6], [4, 6], 'k-')
Corrected Full Code
I also adjusted the title assignment to avoid potential errors—set_title() returns None, so storing the sns.regplot() object first then setting the title on it is safer (otherwise plot23 would be None, which would break the savefig step):
plot23 = sns.regplot(x = Combined['log10(L/L_solar)'], y = Combined['logLum'], fit_reg=False) plot23.set_title('Figure 23: Comparing luminosities') plt.xlabel('logL from my data', fontsize=13) plt.ylabel('logL from Drout', fontsize=13) plt.axis((4, 6, 4, 6)) plt.plot([4, 6], [4, 6], 'k-') # Fixed 1:1 diagonal line plt.show() plot23.figure.savefig("figure23.png")
内容的提问来源于stack exchange,提问作者Lauren

