1. 折线图

绘制一条线:

plot函数来绘制折线图。下面是对各个参数的解释:

x:要绘制折线图的x轴数据。y:要绘制折线图的y轴数据。c(可选):指定折线的颜色。默认为蓝色("b")。marker(可选):指定折线上的标记符号。默认为无标记(无标记线)。markersize(可选):指定标记符号的大小。默认为6。

plt.figure(figsize=(8, 5))

x = ["Mon", "Tues", "Wed", "Thur", "Fri","Sat","Sun"]

y = [20, 40, 35, 55, 42, 80, 50]

plt.plot(x, y, c="g", marker='D', markersize=5)

#绘制坐标轴标签

plt.xlabel("星期")

plt.ylabel("活跃度")

plt.title("迪士尼游乐活跃度")

for x1, y1 in zip(x, y):

plt.text(x1, y1, str(y1), ha='center', va='bottom', fontsize=16)

plt.savefig('images/5-1.png')

plt.show()

zip函数用来同时遍历两个序列x和y。zip函数将x和y中对应位置的元素打包成一个元组,并返回一

个迭代器。在for循环中,通过x1和y1来遍历这个迭代器,分别表示x和y中对应位置的元素。

这段代码适用于需要同时遍历多个序列的情况,例如计算两个序列的元素之和、元素对应位置的差

值等操作。可以根据自己的需求在for循环中进行相应的操作。

 

 绘制多条线:

plt.figure(figsize=(8, 5))

x = np.random.randint(0, 10, size=15)

plt.plot(x, marker='*', color='r')

plt.plot(x.cumsum(), marker='o')

plt.savefig('images/5-2.png')

 Pandas获取Excel数据:

df = pd.read_excel('data/plot.xlsx', sheet_name='line')

df.head()

x, y1, y2, y3 = df['月份'], df['语文'], df['数学'], df['英语']

# 画折线图

# 画布大小

plt.figure(figsize=(7, 4))

# ms: marker size 标记点的大小

# alpha: 透明度 0~1之间,1表式不透明,0表式完全透明

plt.plot(x, y1, label='语文', c='g', ls='--', marker='*', mfc='y', ms=10, alpha=0.5)

plt.plot(x, y2, label='数学', c='b', ls='-.', marker='o', mfc='w', ms=10)

plt.plot(x, y3, label='英语', c='r', ls=':', marker='+', mfc='w', ms=10, alpha=0.5)

plt.yticks(range(0, 110, 10)) # y轴的刻度

plt.ylabel('成绩')

plt.xlabel('月份')

plt.title('成绩的变化趋势')

plt.legend() # 图例

plt.grid(axis='y')

plt.savefig('images/5-3.png')

plt.show()

相关阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: 


大家都在找:

matplotlib:matplotlib安装

python:python是做什么的

开发语言:开发语言排行榜

大家都在看: