为什么在添加散点图时此图失败,但在删除散点图时却起作用? [英] Why does this plot fail when adding scatter but it works when removing it?
问题描述
我正在使用以下内容绘制 x 日期和 y 小数点.它在没有注释行(散点图)的情况下效果很好,但是如果取消注释,它会在一个x轴上全部显示
我想念什么?
谢谢
将matplotlib.pyplot导入为plt导入日期时间fig = plt.figure(facecolor="#979899")ax = plt.gca()ax.set_facecolor("#d1d1d1")plt.grid(真)plt.title(这是标题",fontsize = 16)plt.xticks([datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)],["11/20","11/21," 11/22])plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0],["0.0","0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9","1.0"])x1 = [datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)]y1 = [0.18,0.32,0.21]对于 i,item in enumerate(y1):xP = x1[i]yP = y1[i]plt.text(xP,yP,str(item)+"%",fontsize=11)#plt.scatter(x1,y1)plt.plot(x1,y1)plt.show()
问题是集合的自动缩放.
然而,您可以先创建一个 plot
.这将充分确定自动缩放限制.之后绘制 scatter
.
将matplotlib.pyplot导入为plt导入日期时间无花果,ax = plt.subplots()x1 = [datetime.date(2018,11,20),datetime.date(2018,11,22)]y1 = [1,2]plt.plot(x1,y1, 标记="o", ms=3)plt.scatter(x1,y1, s=50, color="red")plt.show()
I am using the following to plot a x date and y decimal. It works great without the commented line (scatter), but if you uncomment it it shows all in one x axis
what am I missing?
thanks
import matplotlib.pyplot as plt
import datetime
fig = plt.figure(facecolor="#979899")
ax = plt.gca()
ax.set_facecolor("#d1d1d1")
plt.grid(True)
plt.title("This is a title",fontsize=16)
plt.xticks([datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)],["11/20","11/21","11/22"])
plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0],["0.0","0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9","1.0"])
x1 = [datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)]
y1 = [0.18,0.32,0.21]
for i,item in enumerate(y1):
xP = x1[i]
yP = y1[i]
plt.text(xP,yP,str(item)+"%",fontsize=11)
#plt.scatter(x1,y1)
plt.plot(x1,y1)
plt.show()
The problem is the autoscaling of Collections. This is the underlying issue, which is long known, but hard to come by. There are certain cases, where autoscaling does not work reliably, when values are too close to each other. This is unfortunately often the case for dates.
import matplotlib.pyplot as plt
import datetime
fig, ax = plt.subplots()
x1 = [datetime.date(2018,11,20),datetime.date(2018,11,22)]
y1 = [1,2]
plt.scatter(x1,y1, s=50, color="red")
plt.plot(x1,y1, marker="o", ms=3, zorder=3)
plt.show()
You can however create a plot
first. This will determine the autoscaling limits sufficiently. After that plot the scatter
.
import matplotlib.pyplot as plt
import datetime
fig, ax = plt.subplots()
x1 = [datetime.date(2018,11,20),datetime.date(2018,11,22)]
y1 = [1,2]
plt.plot(x1,y1, marker="o", ms=3)
plt.scatter(x1,y1, s=50, color="red")
plt.show()
这篇关于为什么在添加散点图时此图失败,但在删除散点图时却起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!