英文:
Why can't plot the point with format string '-'?
问题
用格式字符串 -
画一条线:
import matplotlib.pyplot as plt
x=[3,4]
y=[4,5]
plt.plot(x,y,"-")
plt.show()
为什么不能用格式字符串 -
画一个点:
为什么在图中用 -
没有显示点?
英文:
Draw a line with format string -
:
import matplotlib.pyplot as plt
x=[3,4]
y=[4,5]
plt.plot(x,y,"-")
plt.show()
Why can't draw a point with format string -
:
Why no point shown with -
in the graph?
答案1
得分: 3
以下是您要翻译的代码部分:
import matplotlib.pyplot as plt
x=[3,4,5,6,7,8]
y=[4,5,6,7,8,9]
plt.scatter(x, y, marker='-', color='black', linewidths=3)
plt.show()
结果:
由于linewidths = 3
,它看起来像是粗体字符。
使用linewidths = 1
或不使用关键字参数,你会得到更像'-'
的效果。
如@Joao_PS建议的那样,使用plt.plot(x, y, marker='$-$')
也可以工作,但plot.plot
函数默认创建的线会覆盖'-'
字符。您可以使用plt.plot(x, y, marker='$+$')
来更好地看到它,参见下面的图片:
有关标记的更多解释,请查看标记的文档:https://matplotlib.org/stable/api/markers_api.html#module-matplotlib.markers
英文:
my attempt:
import matplotlib.pyplot as plt
x=[3,4,5,6,7,8]
y=[4,5,6,7,8,9]
plt.scatter(x,y, marker = '$-$', color ='black', linewidths = 3)
plt.show()
result:
its like bold character because of linewidths = 3
with linewidths = 1
or non using the keyword argument you'll get more like '-'
using, as suggested by @Joao_PS, plt.plot(x, y, marker='$-$')
works to but the '-' char will be covered by the line create by default by plot.plot
func. You could see it better using plt.plot(x, y, marker='$+$')
, see picture below:
for better explanation have a look at documentation about markers :https://matplotlib.org/stable/api/markers_api.html#module-matplotlib.markers
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论