wideband 发表于 2021-9-24 07:19:50

python3.6散点图出不来,输出结果:matplotlib.lines.Line2D object at 0x0

代码:
import os
import matplotlib.pyplot as plt
import numpy as np

print("散点图")
x = np.linspace(0,10,30)
y = np.sin(x)
plt.plot(x,y,'bo',ms =5)
print("散点图2222")


输出结果:

散点图
散点图2222
[<matplotlib.lines.Line2D object at 0x0000017870833518>]


请求支持,谢谢!

小圣呀 发表于 2021-9-24 09:25:13

代码画完图后要展示就后加一句 plt.show()

wideband 发表于 2021-9-24 09:29:41

谢谢 支持; 更改下,可以显示图了,但提示:如后面,这个怎么处理呀?

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,10,30)
y = np.sin(x)
plt.plot(x,y,'bo',label="男生",ms =5)

plt.legend()
plt.show()


/////////////////////////////////////////////////////////////////////////////


Warning (from warnings module):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 238
    font.set_text(s, 0.0, flags=flags)
RuntimeWarning: Glyph 30007 missing from current font.

Warning (from warnings module):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 238
    font.set_text(s, 0.0, flags=flags)
RuntimeWarning: Glyph 29983 missing from current font.

Warning (from warnings module):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 201
    font.set_text(s, 0, flags=flags)
RuntimeWarning: Glyph 30007 missing from current font.

Warning (from warnings module):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 201
    font.set_text(s, 0, flags=flags)
RuntimeWarning: Glyph 29983 missing from current font.

z5560636 发表于 2021-9-24 09:31:01





import os
import matplotlib.pyplot as plt
import numpy as np

print("散点图")
x = np.linspace(0,10,30)
y = np.sin(x)
plt.plot(x,y,'bo',ms =5)
print("散点图2222")
plt.show()

如楼上所说,价格plt.show() 就能出来了。

wideband 发表于 2021-9-24 09:37:51

z5560636 发表于 2021-9-24 09:31
如楼上所说,价格plt.show() 就能出来了。

谢谢,是出图了,但是出现了很多警告呢:


Warning (from warnings module):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 238
    font.set_text(s, 0.0, flags=flags)
RuntimeWarning: Glyph 30007 missing from current font.

Warning (from warnings module):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 238
    font.set_text(s, 0.0, flags=flags)
RuntimeWarning: Glyph 29983 missing from current font.

Warning (from warnings module):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 201
    font.set_text(s, 0, flags=flags)
RuntimeWarning: Glyph 30007 missing from current font.

Warning (from warnings module):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 201
    font.set_text(s, 0, flags=flags)
RuntimeWarning: Glyph 29983 missing from current font.

小圣呀 发表于 2021-9-24 09:44:05

wideband 发表于 2021-9-24 09:29
谢谢 支持; 更改下,可以显示图了,但提示:如后面,这个怎么处理呀?

import numpy as np


应该是因为有中文乱码了。可以把男孩改成boy,避免出现中文就行
或者增加以下代码
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['KaiTi', 'SimHei', 'FangSong']# 设置汉字字体
mpl.rcParams['font.size'] = 12# 字体大小
mpl.rcParams['axes.unicode_minus'] = False# 正常显示负号

wideband 发表于 2021-9-24 10:05:55

小圣呀 发表于 2021-9-24 09:44
应该是因为有中文乱码了。可以把男孩改成boy,避免出现中文就行
或者增加以下代码

谢谢你的支持,修改了,还是不行,提示如下:

import os
import numpy as np
import matplotlib.pyplot as plt

import matplotlib as mpl
mpl.rcParams['font.san-serif']=['KaiTi','SimHei','FangSong']
mpl.rcParams['font.size']=12
mpl.rcParams['axes.unicode_minus'] = False



x = np.linspace(0,10,30)
y = np.sin(x)
plt.plot(x,y,'bo',label="man",ms =5)

plt.legend()
plt.show()


//////////////////////////////////////////////////////////////////////////

import os
import numpy as np
import matplotlib.pyplot as plt

import matplotlib as mpl
mpl.rcParams['font.san-serif']=['KaiTi','SimHei','FangSong']
mpl.rcParams['font.size']=12
mpl.rcParams['axes.unicode_minus'] = False



x = np.linspace(0,10,30)
y = np.sin(x)
plt.plot(x,y,'bo',label="man",ms =5)

plt.legend()
plt.show()

wideband 发表于 2021-9-24 10:07:43

//////////////////////////////////后面的提示警告如下////////////////////////////////

Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\__init__.py", line 677, in __setitem__
    cval = self.validate(val)
KeyError: 'font.san-serif'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/sandian.py", line 6, in <module>
    mpl.rcParams['font.san-serif']=['KaiTi','SimHei','FangSong']
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\__init__.py", line 684, in __setitem__
    f"a list of valid parameters)") from err
KeyError: 'font.san-serif is not a valid rc parameter (see rcParams.keys() for a list of valid parameters)'

wideband 发表于 2021-9-24 10:08:57

现在可以了,我删除了如下,其他保留了,谢谢!

import matplotlib as mpl
mpl.rcParams['font.san-serif']=['KaiTi','SimHei','FangSong']
mpl.rcParams['font.size']=12
mpl.rcParams['axes.unicode_minus'] = False

小圣呀 发表于 2021-9-24 10:21:27

wideband 发表于 2021-9-24 10:08
现在可以了,我删除了如下,其他保留了,谢谢!

import matplotlib as mpl


你这个mpl.rcParams['font.sans-serif']=['KaiTi','SimHei','FangSong'],sans掉了个s……

输入中文字是可以的,再试试?
import os
import numpy as np
import matplotlib.pyplot as plt

import matplotlib as mpl
mpl.rcParams['font.sans-serif']=['KaiTi','SimHei','FangSong']
mpl.rcParams['font.size']=12
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0,10,30)
y = np.sin(x)
plt.plot(x,y,'bo',label="男孩",ms =5)

plt.legend()
plt.show()

wideband 发表于 2021-9-24 10:27:07

小圣呀 发表于 2021-9-24 10:21
你这个mpl.rcParams['font.sans-serif']=['KaiTi','SimHei','FangSong'],sans掉了个s……

输入中文字 ...

你是谁呀?是小甲鱼老师吗? 你这么牛,谢谢你,安全OK了。

import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif']=['KaiTi','SimHei','FangSong']
mpl.rcParams['font.size']=12
mpl.rcParams['axes.unicode_minus']=False

x = np.linspace(0,10,30)
y = np.sin(x)
plt.plot(x,y,'bo',label="man",ms =5)

plt.legend()
plt.show()

页: [1]
查看完整版本: python3.6散点图出不来,输出结果:matplotlib.lines.Line2D object at 0x0