楼主: snile
942 3

[问答] 请问如何通过Python语言画出如图所示的,带图例的折线图。 [推广有奖]

  • 0关注
  • 0粉丝

大专生

28%

还不是VIP/贵宾

-

威望
0
论坛币
19 个
通用积分
0.1198
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
426 点
帖子
23
精华
0
在线时间
46 小时
注册时间
2017-12-8
最后登录
2023-1-19

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
请问如何通过Python语言画出如图所示的,带图例的折线图。
请大神指教!十分感谢! 2021-10-18_210707.jpg

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python语言 python 如图所示 折线图

沙发
xpz186 发表于 2021-10-20 17:07:42 |只看作者 |坛友微信交流群
matplotlib中设置marker属性。

使用道具

藤椅
snile 发表于 2021-10-25 16:50:58 |只看作者 |坛友微信交流群
xpz186 发表于 2021-10-20 17:07
matplotlib中设置marker属性。
感谢大佬指教,假设我有以下的数据,请问具体该如何编写代码,实现问题中的截图呢?
df = pd.DataFrame({
    'group': [0, 1000, 2000, 3000, 4000, 5000, 6000, 7000],
    'ACC': [0.8001, 0.8101, 0.8202, 0.8306, 0.8605, 0.8453, 0.8788, 0.8966],
    'NMI': [0.8002, 0.8103, 0.8232, 0.8406, 0.8505, 0.8400, 0.8701, 0.8506],
    'ARI': [0.8003, 0.8133, 0.8242, 0.8206, 0.8105, 0.8100, 0.8601, 0.8306],
})

使用道具

板凳
司马芋 发表于 2021-11-9 14:58:28 |只看作者 |坛友微信交流群
试下并修改这个,低手,很不简洁
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

df = pd.DataFrame({
    'group': [0, 1000, 2000, 3000, 4000, 5000, 6000, 7000],
    'ACC': [0.8001, 0.8101, 0.8202, 0.8306, 0.8605, 0.8453, 0.8788, 0.8966],
    'NMI': [0.8002, 0.8103, 0.8232, 0.8406, 0.8505, 0.8400, 0.8701, 0.8506],
    'ARI': [0.8003, 0.8133, 0.8242, 0.8206, 0.8105, 0.8100, 0.8601, 0.8306],
})

df2=df.set_index('group').cumsum()

xmjlocator=MultipleLocator(1000)
xmnlocator=MultipleLocator(500)
ymjlocator=MultipleLocator(0.1)
ymnlocator=MultipleLocator(0.05)

yerr=(np.random.randn(1,8)/50).flatten()
markers=['v','o','s']
cols=['ACC','NMI','ARI']
colors=['b','g','y']
fig,axs=plt.subplots(1,2)
print(type(fig))
for col,mk,color in zip(cols,markers,colors):
    axs[0].plot(df['group'],df[col],marker=mk,color=color)
axs[0].set_ylim([0.46,1.04])
axs[0].set_xlabel('x1')
axs[0].xaxis.set_minor_locator(xmnlocator)
axs[0].yaxis.set_minor_locator(ymnlocator)
axs[0].grid(which='major',axis='both',color='grey',linewidth=3)
axs[0].grid(which='minor',axis='both',color='grey',linewidth=1)
axs[0].spines['right'].set_visible(False)
axs[0].spines['left'].set_visible(False)
axs[0].spines['top'].set_visible(False)
axs[0].spines['bottom'].set_visible(False)
axs[0].tick_params(direction='out',length=0,width=4,colors='grey',
                   grid_color='grey',grid_alpha=.5,rotation=60,
                   axis='x')
axs[0].tick_params(direction='out',length=0,width=4,colors='grey',
                   grid_color='grey',grid_alpha=.5,
                   axis='y')
axs[0].tick_params(direction='in',length=0,width=1,colors='grey')

for col,mk,color in zip(cols,markers,colors):
    axs[1].plot(df['group'],df[col],marker=mk,color=color)
    axs[1].errorbar(df['group'],df[col],yerr=yerr,elinewidth=3,
                    color=color,fmt='_',capsize=4)
axs[1].set_ylim([0.46,1.04])
axs[1].set_xlabel('x2')
axs[1].xaxis.set_minor_locator(xmnlocator)
axs[1].yaxis.set_minor_locator(ymnlocator)
axs[1].grid(which='major',axis='both',color='grey',linewidth=3)
axs[1].grid(which='minor',axis='both',color='grey',linewidth=1)
axs[1].spines['right'].set_visible(False)
axs[1].spines['left'].set_visible(False)
axs[1].spines['top'].set_visible(False)
axs[1].spines['bottom'].set_visible(False)
axs[1].tick_params(direction='out',length=0,width=4,colors='grey',
                   grid_color='grey',grid_alpha=.5,rotation=60,
                   axis='x')
axs[1].tick_params(direction='out',length=0,width=4,colors='grey',
                   grid_color='grey',grid_alpha=.5,
                   axis='y')
axs[1].tick_params(direction='in',length=0,width=1,colors='grey')


plt.legend(labels=['ACC','NMI','ARI'],
           bbox_to_anchor=(-0.6,-0.25,1,0.2),
           loc='lower center',
           mode='expand',
           ncol=3,
           columnspacing=1.0,
           frameon=False)
plt.tight_layout()
plt.show()

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-26 14:46