我刚接触python,我尝试用matplotlib创建一个图表,我可以创建第一行,没有问题,我定义了两个长度相同的x和y值列表,对于第二行,我尝试绘制一个图表,显示第一行中每个x的最大y值,我的问题是,我必须显式地写max吗(y1)x次,或者是否有方法自动化该过程?
x1 = [8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
y1 = [36.37, 36.18, 36.31, 36.24, 36.31, 36.37, 36.18, 36.43, 36.12, 36.24, 36.06, 36.31, 36.12, 36.49, 36.74, 36.74]
plt.plot(x1, y1, color='black', marker='o', markerfacecolor='black', markersize='10', label='line1')
# I could just write x1 but this was a little easier for me to structure my code
x2 = x1
# Do I have to write [max(y1), max(y1), max(y1) ..., max(y1)] here or can I make a list with fewer items to represent one line (in this case y=36.74)
y2 = [max(y1)]
plt.plot(x2, y2, color='#30C3BF', linestyle='dashed', label='line2')
1条答案
按热度按时间iq0todco1#
您可以将列表相乘。
生成与y1长度相同的单个列表,其中所有值都设置为y1中的最大值