简体   繁体   English

Matplotlib 在一个散点图中绘制两个数据框

[英]Matplotlib plotting two dataframes in one scatter plot

I am able to plot two indivdiual scatter plots for df5 and df8 using this but I can't seem to plot them on the same figure.我可以使用它为 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)

To plot them on same figure I tried the following but it does not work, it gives a straight line, both the df has same number of rows and columns要将它们绘制在同一个图上,我尝试了以下操作,但它不起作用,它给出了一条直线,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')

You may want to use the build-in matplotlib-based plotting of pandas right away.您可能想立即使用内置的基于 matplotlib 的 Pandas 绘图。 The only important note is that you need to tell matplotlib to which axis it is supposed to draw the points:唯一重要的注意事项是您需要告诉 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')

If you insist on using matplotlib all the way, open a figure with an axis and plot to the same axis:如果你一直坚持使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM