简体   繁体   中英

using matplotlib to do a scatter plot

when I try to use matplotlib to plot the code i see an empty figure with no plots on it. I am attaching the code and the blank figure. Please let me know as to what I am missing. Thanks!

empty window with no plot

from datetime import datetime
start_time = datetime.now()
print(start_time)

import pandas as pd
import numpy as np

file1 = 'fn_data.csv'
import matplotlib.pyplot as plt
#import pylab

# Read the .txt file into a dataframe
data = pd.read_csv(file1, encoding = "ISO-8859-1", header=0, delimiter=',')
rating=data.iloc[:,0]
chef=data.iloc[:,3]
print(rating)

mydict={}
i = 0
for item in chef:
    if(i>0 and item in mydict):
        continue
    else:
       i = i+1
       mydict[item] = i

chef_codes=[]
for item in chef:
    chef_codes.append(mydict[item])

print(chef_codes)
chef_codes_new=np.array(chef_codes)
rating_new=np.array(rating)
print(type(chef_codes_new),type(rating_new))
print(np.max(chef_codes_new),np.max(rating_new))
plt.plot(kind='scatter',x=chef_codes_new,y=rating_new, marker='o', ms = 10, alpha=1, color='b')
plt.axis([0, 1000, 0, 5])
plt.show()
plt.savefig("fig1.png")


end_time = datetime.now()
print(end_time)
def scatterPlot(X,Y):
    ids= ['green' if y == 0 else 'red' for y in Y]
    plt.scatter(X[:,0], X[:,1], color=ids)
    plt.title("Scatter Plot")
    return

scatterPlot(x,y)

Its only an example of scatter plot hope it might help you out from your problem.

If plt.scatter works, plt.plot will work as well. The problem is that you have mixed the syntax of pandas.DataFrame.plot with the command of pyplot.plot .

So, instead of

plt.plot(kind='scatter',x=chef_codes_new,y=rating_new, marker='o', ms = 10, alpha=1, color='b')

you need

plt.plot(chef_codes_new,rating_new, marker='o', ms = 10, alpha=1, color='b')

The syntax is plt.plot(x,y, *args, **kwargs) and you cannot use kind="scatter" in a pyplot plot. If you want a scatter plot, use plt.scatter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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