You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Matplotlib折线图线条呈平滑圆角而非尖锐转角?

问题:折线图线条连接处尖锐,如何设置圆角连接?

我希望制作出美观的图表,让折线图的线条以圆角方式连接,但目前线条的连接处是尖锐的。请问我的代码存在什么问题?缺少了哪个参数?

我的代码

import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler
import pandas as pd

def data_gr():
    fr = pd.read_csv('ramka_wynikowa.csv', sep=';')
    merged_df_database = fr

    # 设置线条颜色
    line_colors = ['#8497B0', '#F6DD60', '#06A297']
    marker_colors = ['#5E7594', '#F2CD16', '#047068']

    # 创建柱状图
    fig, ax = plt.subplots()
    ax.bar(merged_df_database.index, merged_df_database['quantity'], color='gray', alpha=0.6, edgecolor='none', width=0.8, label='quantity', capstyle='round')

    # 添加折线图数据
    for i, col in enumerate(['value', 'value_up', 'rebate_value']):
        ax.plot(merged_df_database.index, merged_df_database[col], lw=4, solid_capstyle='round', color=line_colors[i], marker='o', markerfacecolor=marker_colors[i], markeredgecolor=marker_colors[i], linestyle='-', label=col)

    # 添加图例和标题
    ax.legend()
    ax.set_title('数据图表')

    # 添加数据标签
    for x, y, q in zip(merged_df_database.index, merged_df_database['quantity'], merged_df_database['quantity']):
        ax.text(x, y, '{:.1f}'.format(q/1000), ha='center', va='bottom', fontsize=10)

    for col in ['value', 'value_up', 'rebate_value']:
        for x, y, v in zip(merged_df_database.index, merged_df_database[col], merged_df_database[col]):
            ax.text(x, y, '{:.1f}'.format(v/1000), ha='center', va='bottom')

    ax.set_yticks(ax.get_yticks())
    ax.set_yticklabels([f'{int(t / 1000)}' for t in ax.get_yticks()])
    plt.show()

当前生成的图表

当前折线图效果

样本数据

invoice_num;salesman_id;market;quantity;value;customer;fv_date;surname;value_up;rebate_value;price_per_unit;;;
1001152;18;GE;7500.0;9005.12;BA125;12.01.2024;Odp;9725.529600000002;9005.12;1.2006826666666668;;;"7500.0;1.2"
1001154;12;GE;8200.0;9844.88;BA118;22.01.2024;Szy;10632.4704;9844.88;1.2005951219512194;;;"8200.0;1.2"
1001157;18;GE;6500.0;11712.42;AA088;25.01.2024;Odp;12649.413600000002;11712.42;1.8019107692307692;;;"6500.0;1.8"
1001159;18;GE;10300.0;19574.55;AC098;02.02.2024;Odp;21140.514;17617.095;1.9004417475728155;;;"10300.0;1.9"
1001161;18;GE;6900.0;11734.05;AD104;12.02.2024;Odp;12672.774;11734.05;1.700586956521739;;;"6900.0;1.7"
1001164;12;GE;12000.0;14410.85;BA138;15.02.2024;Szy;15563.718;12969.765000000001;1.2009041666666667;;;"12000.0;1.2"
1001166;12;GE;12300.0;25832.22;AF105;27.02.2024;Szy;27898.7976;23248.998000000003;2.100180487804878;;;"12300.0;2.1"
1001169;12;GE;10800.0;20526.42;AG222;03.03.2024;Szy;22168.5336;18473.778;1.9005944444444443;;;"10800.0;1.9"
1001171;18;GE;7500.0;13521.88;BA118;11.03.2024;Odpow;14603.6304;13521.88;1.8029173333333333;;;"7500.0;1.8"
1001174;18;GE;6900.0;11730.14;AH111;16.03.2024;Odpow;12668.5512;11730.14;1.7000202898550725;;;"6900.0;1.7"

解决方案:添加joinstyle='round'参数

你当前使用的solid_capstyle='round'仅控制线条两端的圆角样式,而折线转折点的圆角需要通过joinstyle='round'参数来设置,这就是你缺失的关键参数。

修改后的折线图绘制代码

将ax.plot()的参数中加入joinstyle='round',修改后的代码行如下:

ax.plot(merged_df_database.index, merged_df_database[col], lw=4, solid_capstyle='round', joinstyle='round', color=line_colors[i], marker='o', markerfacecolor=marker_colors[i], markeredgecolor=marker_colors[i], linestyle='-', label=col)

额外优化建议

你代码中导入的interp1d、KMeans、MinMaxScaler三个库并未实际使用,可以直接删除,减少代码冗余。


内容的提问来源于stack exchange,提问作者user21219510

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 07:07:22