首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绘制两个数据序列

绘制两个数据序列
EN

Stack Overflow用户
提问于 2019-05-19 14:22:50
回答 1查看 32关注 0票数 0

我是Pythion的新手,我想简单地知道如何使用plot函数在图形中绘制以下代码产生的一系列数据?

我希望x轴是some_function的结果,y轴是t1的结果。

这是一个作业,我只能使用plot,而不是matplotlib,因为我们没有学过。

谢谢

代码语言:javascript
复制
from pylab import *

def some_function(ff, dd):
    if dd >=0 and dd <=300:
        tt = (22/-90)*ff+24
    elif dd >=300 and dd <=1000:
        st = (22/-90)*(ff)+24
        gg = (st-2)/-800
        tt = gg*dd+(gg*-1000+2)
    else:
        tt = 2.0
    return tt

t1=arange(0,12000,1000)

print(t1)

for x in t1: 
    print(some_function(55,x))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-19 15:48:58

我不确定你是想要散点图,还是折线图,所以我把这两个选项都包括进来了。

代码语言:javascript
复制
from pylab import *
import matplotlib.pyplot as plt

def some_function(ff, dd):
    if dd >=0 and dd <=300:
        tt = (22/-90)*ff+24
    elif dd >=300 and dd <=1000:
        st = (22/-90)*(ff)+24
        gg = (st-2)/-800
        tt = gg*dd+(gg*-1000+2)
    else:
        tt = 2.0
    return tt

t1=arange(0,12000,1000)

x_data = [some_function(55,x) for x in t1]
y_data = t1

# Scatter plot
plt.scatter(x_data, y_data)
# Line plot
plt.plot(x_data, y_data)

plt.show()

#Optionally, you can save the figure to a file.
plt.savefig("my_plot.png")

如果您确实不能直接使用matplotlib,只需运行:

代码语言:javascript
复制
# Scatter plot
scatter(x_data, y_data)
# Line plot
plot(x_data, y_data)

show()

savefig('my_plot.png')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56205290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档