无法绘制带有格式字符串’-‘的点吗?

huangapple go评论52阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月13日 15:21:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定