简体   繁体   English

使用matplotlib进行散点图

[英]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. 当我尝试使用matplotlib绘制代码时,我看到一个没有图形的空图。 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. 它只是scatter plot一个示例,希望它可以帮助您解决问题。

If plt.scatter works, plt.plot will work as well. 如果plt.scatter有效,则plt.plot也将有效。 The problem is that you have mixed the syntax of pandas.DataFrame.plot with the command of pyplot.plot . 问题是您将pandas.DataFrame.plot的语法与pandas.DataFrame.plot的命令混合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. 语法为plt.plot(x,y, *args, **kwargs)并且不能在pyplot图中使用kind="scatter" If you want a scatter plot, use plt.scatter . 如果要散点图,请使用plt.scatter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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