[英]Matplotlib plotting two dataframes in one scatter plot
我可以使用它為 df5 和 df8 繪制兩個單獨的散點圖,但我似乎無法將它們繪制在同一個圖上。
# plot
plt.plot( 'Temperature', 'Moment', data=df5, linestyle='none', marker='o', alpha=0.4, markerfacecolor='lightblue',
markersize='6')
plt.rcParams["font.family"] ="sans-serif"
plt.rcParams["font.size"] = 10
plt.xlabel('Temperature(K)')
plt.ylabel('DC Moment (emu)')
fig = plt.figure()
fig.set_figheight(15)
fig.set_figwidth(10)
要將它們繪制在同一個圖上,我嘗試了以下操作,但它不起作用,它給出了一條直線,df 具有相同的行數和列數
x1= df5['Temperature']
y1= df5['Moment']
x2= df8['Temperature']
y2= df8['Moment']
plt.scatter(x1, y1, color= 'g')
plt.scatter(x2, y2, color='b')
您可能想立即使用內置的基於 matplotlib 的 Pandas 繪圖。 唯一重要的注意事項是您需要告訴 matplotlib 應該將點繪制到哪個軸:
from matplotlib import pyplot as plt
# open figure/axis
fig, ax = plt.subplots()
# plot // you need to specify the axis
df5.plot(x='Temperature', y='Moment', kind='scatter', ax=ax, c='g')
df8.plot(x='Temperature', y='Moment', kind='scatter', ax=ax, c='b')
如果你一直堅持使用 matplotlib,打開一個帶軸的圖形,繪制到同一軸上:
# open figure/axis
fig, ax = plt.subplots()
ax .scatter(x1, y1, color='g')
# use the same axis object to plot the other values
ax .scatter(x2, y2, color='b')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.