python - 如何在 y 轴上获得正确的顺序(matplotlob(P 2.7)正确
问题描述
我想知道为什么y轴的顺序是随机的(见截图)。我正在绘制带有值对的文本文件中的 x 和 y 值,并且希望 y 轴从最小值开始并以最大值结束。在我的例子中,y 轴图的中间有最小的数字 (3)。我该如何纠正这个错误?
txt 文件:
1,5
2,3
3,5
4,3
5,7
6,3
7,5
8,3
9,5
10,3
代码:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
style.use('fivethirtyeight')
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
def animate(i):
graph_data = open('example.txt','r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(y)
ax1.clear()
ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig, animate, interval=1000) # 1000 ms = 1 sec (updating)
plt.show()
我发现编辑以下两行代码有帮助!!!
之前: xs.append(x) ys.append(y)
之后: xs.append(float(x)) ys.append(float(y))
解决方案
您在行上指定了 FiveThirtyEight 样式
style.use('fivethirtyeight')
只需将其删除即可使用正常的绘图样式。
推荐阅读
- planning - 在 PDDL 2.1 中分配除法值,公制 FF
- python - 谷歌浏览器如何处理 html 页面?
- asp.net-mvc - 托管 ASP.NET MVC 应用程序时,Serilog 不登录数据库
- r - 在 R 中使用 ggplot2 为具有不同渐变的单行着色
- c# - 有没有办法让数组保存 blazor 组件?
- c# - FtpWebRequest 到微控制器总是导致 (451) 处理中的本地错误
- symfony - 如何在 github 上推送你的项目
- maven - 使用 Maven 运行测试而无需多次构建项目
- python - Python socket.recv() 数据到 Pandas DataFrame DF:有更好的工作流程吗?
- html - 使图像不可拖动但仍可点击/悬停