0

I have a dataframe as follows:

    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

I am wanting to plot the cost vs the volume. How would I plot each point with a different colour and use the metal column as the legend.

1
  • 1
    And please next time asking, provide some sample dataframe with the code, it took me 5 minutes to reproduce the dataframe and 2 minutes to write the answer. Commented Apr 26, 2017 at 11:38

1 Answer 1

2

You can iterate over the rows of the dataframe and use pyplot.scatter to plot the points.

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()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.