Python 从数据帧绘制散点图
我有一个数据框,如下所示:Python 从数据帧绘制散点图,python,pandas,matplotlib,plot,scatter-plot,Python,Pandas,Matplotlib,Plot,Scatter Plot,我有一个数据框,如下所示: Metal Cost per m^3/$ volume/mm^3 0 Cast Iron 5996.0 20088.253323 1 Medium Carbon Steel 4301.0 12636.050310 2 Alloy Steel 6490.6 9134.9753
Metal Cost per m^3/$ volume/mm^3
0 Cast Iron 5996.0 20088.253323
1 Medium Carbon Steel 4301.0 12636.050310
2 Alloy Steel 6490.6 9134.975311
3 Stainless Steels 34621.0 29216.210066
8 Titanium Alloys 76500.0 16303.954297
我想画出成本与产量的对比图。
如何用不同的颜色绘制每个点,并使用金属柱作为图例。您可以迭代数据帧的行,并使用
pyplot.scatter
绘制点
import pandas as pd
import matplotlib.pyplot as plt
a = ["Cast Iron", "Medium Carbon Steel", "Alloy Steel",
"Stainless Steels", "Titanium Alloys"]
b = [5996,4301, 6490,34621,76500]
c = [ 20088.253323, 12636.050310, 9134.975311, 29216.210066,16303.954297]
df = pd.DataFrame({"Metal":a, "cost":b, "volume":c})
for row in df.iterrows():
plt.scatter(row[1]["cost"], row[1]["volume"],
c=plt.cm.jet(row[0]/float(len(df))), label=row[1]["Metal"])
plt.legend()
plt.show()
您可以迭代数据帧的行,并使用
pyplot.scatter
绘制点
import pandas as pd
import matplotlib.pyplot as plt
a = ["Cast Iron", "Medium Carbon Steel", "Alloy Steel",
"Stainless Steels", "Titanium Alloys"]
b = [5996,4301, 6490,34621,76500]
c = [ 20088.253323, 12636.050310, 9134.975311, 29216.210066,16303.954297]
df = pd.DataFrame({"Metal":a, "cost":b, "volume":c})
for row in df.iterrows():
plt.scatter(row[1]["cost"], row[1]["volume"],
c=plt.cm.jet(row[0]/float(len(df))), label=row[1]["Metal"])
plt.legend()
plt.show()
如果有帮助的话,请考虑/举出一个答案,它也将表明你的问题已经被回答了,请下次问,提供一些样例数据代码,我花了5分钟来复制数据文件,写了2分钟的答案。如果有帮助的话,请考虑/举出一个答案——它也将表明你的问题已经被回答了,请下次问,提供一些示例数据文件,我花了5分钟重现数据帧,花了2分钟写出答案。