首页 > 解决方案 > Python 绘图图

问题描述

from matplotlib.pyplot import *
list = [1,3,5,7,2,4,6,8,10]
plot(list)

使用上面的代码,代码将绘制 x 轴从 0 到 8 的图形。如果我希望我的 x 轴从另一个值运行,我该怎么办?即我的 x 轴从 120-128 运行?

标签: pythonmatplotlib

解决方案


您可以只使用该show()功能来显示图表。您的代码应该看起来像这样。

from matplotlib.pyplot import *
list = [1,3,5,7,2,4,6,8,10]
plot(list)
show()

而且,正如@BcK 的评论中所提到的,不要使用关键字或内置函数名称作为变量名,最后不要import *,这不是一个好习惯。您可以将整个程序更新为:

import matplotlib.pyplot as plt
var_list =  [1,3,5,7,2,4,6,8,10] # don't use keywords or inbuilt function names as varible names 
plt.plot(var_list)
plt.show()  # this is to visualize the plot 

推荐阅读