pandas 减少图形x轴上的ticklabels的数量,同时保持曲线与所有 Dataframe 线

svmlkihl  于 2023-05-05  发布在  其他
关注(0)|答案(1)|浏览(214)

在问题的最后,有一个CSV文件的例子,其数据从2023年4月17日到2023年4月30日的记录开始,在“open_local_data”列中。
请注意,当尝试生成图形点时,由于日期和时间列的格式,它们被重置为1970。但是,如果我将列转换为Pandas提供的日期模型,图形线将自动更改为仅几个每日点,但我希望保留所有唯一点,仅减少x轴上的视觉标记。
我该怎么办?

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.dates as mdates

def chart():
    df_chart = pd.read_csv('example.csv')
    fig, ax = plt.subplots(figsize=(10.8, 10.8))
    ax.plot(df_chart['open_local_data'], df_chart['cumulative_back'], linestyle='-', linewidth=2, color='blue')

    def format_ytick(value, pos):
        if value.is_integer():
            return f'{int(value)}u'
        else:
            return f'{value:.2f}u'

    ax.yaxis.set_major_formatter(ticker.FuncFormatter(format_ytick))

    ax.set_title('Example', color='white')
    ax.set_xlabel('Data', color='white')
    ax.set_ylabel('Profit & Loss', color='white')

    locator = mdates.AutoDateLocator()
    formatter = mdates.ConciseDateFormatter(locator)
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(formatter)

    ax.set_facecolor('black')
    ax.grid(color='orange', linewidth=0.5)
    ax.spines['bottom'].set_color('orange')
    ax.spines['bottom'].set_linewidth(2)
    ax.spines['left'].set_color('orange')
    ax.spines['left'].set_linewidth(2)
    ax.tick_params(colors='white')

    plt.savefig('example_chart.png', facecolor='black', dpi=500)

if __name__ == '__main__':
    chart()

在我的尝试中,如果我尝试将列转换为Pandas的日期格式,图形的直线和曲线并不忠实于数据框架的所有158行,图形中的点数量减少,我不希望发生这种情况,我只希望更改图例以更好地可视化:

df['open_local_data'] = pd.to_datetime(df['open_local_data'])

open_local_data,back,cumulative_back
2023-04-17 15:45:00,-1.0,-1.0
2023-04-17 16:00:00,0.68255,-0.31745
2023-04-17 16:00:00,-1.0,-1.31745
2023-04-18 15:45:00,0.7199500000000001,-0.5974999999999999
2023-04-18 15:45:00,0.21505,-0.38244999999999996
2023-04-18 15:45:00,1.0098,0.6273500000000001
2023-04-18 15:45:00,-1.0,-0.3726499999999999
2023-04-18 15:45:00,0.8976000000000001,0.5249500000000001
2023-04-18 16:00:00,-1.0,-0.47504999999999986
2023-04-18 16:00:00,1.7017,1.2266500000000002
2023-04-19 14:30:00,0.92565,2.1523000000000003
2023-04-19 15:45:00,1.0472,3.1995000000000005
2023-04-19 15:45:00,-1.0,2.1995000000000005
2023-04-19 15:45:00,2.9920000000000004,5.191500000000001
2023-04-19 16:00:00,-1.0,4.191500000000001
2023-04-19 16:00:00,-1.0,3.1915000000000013
2023-04-19 16:00:00,0.4114,3.6029000000000013
2023-04-19 16:00:00,-1.0,2.6029000000000013
2023-04-20 13:45:00,-1.0,1.6029000000000013
2023-04-20 14:30:00,-1.0,0.6029000000000013
2023-04-20 16:00:00,-1.0,-0.3970999999999987
2023-04-20 16:00:00,-1.0,-1.3970999999999987
2023-04-20 16:00:00,1.0659,-0.3311999999999986
2023-04-20 16:00:00,0.5236000000000001,0.19240000000000146
2023-04-20 16:00:00,0.7854000000000001,0.9778000000000016
2023-04-21 15:30:00,-1.0,-0.022199999999998443
2023-04-21 15:45:00,1.8326,1.8104000000000016
2023-04-21 16:00:00,-1.0,0.8104000000000016
2023-04-21 16:00:00,-1.0,-0.18959999999999844
2023-04-21 16:00:00,0.1589499999999999,-0.03064999999999854
2023-04-22 08:00:00,-1.0,-1.0306499999999985
2023-04-22 08:30:00,1.3277,0.2970500000000016
2023-04-22 09:00:00,1.3464,1.6434500000000016
2023-04-22 10:00:00,-1.0,0.6434500000000016
2023-04-22 10:30:00,-1.0,-0.35654999999999837
2023-04-22 11:00:00,1.4025,1.0459500000000017
2023-04-22 11:00:00,-1.0,0.04595000000000171
2023-04-22 11:00:00,-1.0,-0.9540499999999983
2023-04-22 11:00:00,-1.0,-1.9540499999999983
2023-04-22 11:00:00,-1.0,-2.9540499999999983
2023-04-22 11:00:00,-1.0,-3.9540499999999983
2023-04-22 11:00:00,0.3926999999999999,-3.5613499999999982
2023-04-22 11:00:00,-1.0,-4.561349999999998
2023-04-22 11:00:00,0.9537,-3.6076499999999982
2023-04-22 11:15:00,0.6732,-2.9344499999999982
2023-04-22 12:45:00,0.2244,-2.710049999999998
2023-04-22 13:00:00,-1.0,-3.710049999999998
2023-04-22 13:30:00,0.5797000000000001,-3.130349999999998
2023-04-22 15:45:00,-1.0,-4.130349999999998
2023-04-22 16:00:00,1.0846000000000002,-3.045749999999998
2023-04-22 16:00:00,0.27115,-2.774599999999998
2023-04-22 16:30:00,0.2057,-2.5688999999999984
2023-04-23 08:00:00,-1.0,-3.5688999999999984
2023-04-23 08:30:00,0.73865,-2.8302499999999986
2023-04-23 09:00:00,0.8789,-1.9513499999999986
2023-04-23 09:30:00,1.1033000000000002,-0.8480499999999984
2023-04-23 10:00:00,-1.0,-1.8480499999999984
2023-04-23 10:00:00,1.5521000000000005,-0.29594999999999794
2023-04-23 10:00:00,0.9163,0.6203500000000021
2023-04-23 10:00:00,-1.0,-0.37964999999999793
2023-04-23 10:00:00,-1.0,-1.379649999999998
2023-04-23 10:30:00,0.8134500000000001,-0.5661999999999979
2023-04-23 11:15:00,0.9537,0.38750000000000207
2023-04-23 12:30:00,-1.0,-0.6124999999999979
2023-04-23 12:30:00,-1.0,-1.612499999999998
2023-04-23 13:00:00,0.5516500000000001,-1.060849999999998
2023-04-23 13:00:00,-1.0,-2.060849999999998
2023-04-23 14:00:00,0.1402499999999999,-1.920599999999998
2023-04-23 15:45:00,1.6269000000000002,-0.29369999999999785
2023-04-23 16:00:00,-1.0,-1.2936999999999979
2023-04-24 14:00:00,-1.0,-2.2936999999999976
2023-04-24 14:00:00,-1.0,-3.2936999999999976
2023-04-24 15:45:00,1.4025,-1.8911999999999975
2023-04-24 16:00:00,1.1220000000000003,-0.7691999999999972
2023-04-24 16:15:00,0.4114,-0.35779999999999723
2023-04-25 14:30:00,-1.0,-1.3577999999999972
2023-04-25 14:30:00,-1.0,-2.3577999999999975
2023-04-25 15:30:00,1.2903,-1.0674999999999975
2023-04-25 15:45:00,0.92565,-0.14184999999999748
2023-04-25 15:45:00,0.7106,0.5687500000000025
2023-04-25 16:00:00,-1.0,-0.43124999999999747
2023-04-25 16:00:00,1.1781,0.7468500000000025
2023-04-25 16:00:00,-1.0,-0.25314999999999754
2023-04-25 16:00:00,-1.0,-1.2531499999999975
2023-04-25 17:00:00,-1.0,-2.2531499999999975
2023-04-26 14:30:00,-1.0,-3.2531499999999975
2023-04-26 14:30:00,0.3272500000000001,-2.9258999999999973
2023-04-26 15:00:00,0.5236000000000001,-2.402299999999997
2023-04-26 15:30:00,4.581500000000001,2.179200000000004
2023-04-26 15:30:00,-1.0,1.1792000000000038
2023-04-26 15:45:00,-1.0,0.1792000000000038
2023-04-26 15:45:00,1.0285000000000002,1.207700000000004
2023-04-26 16:00:00,0.90695,2.114650000000004
2023-04-26 16:00:00,0.9537,3.068350000000004
2023-04-26 16:00:00,0.66385,3.732200000000004
2023-04-26 17:00:00,-1.0,2.732200000000004
2023-04-26 17:00:00,-1.0,1.7322000000000042
2023-04-27 14:30:00,0.53295,2.265150000000004
2023-04-27 14:30:00,0.68255,2.947700000000004
2023-04-27 15:45:00,0.8228,3.770500000000004
2023-04-27 15:45:00,-1.0,2.770500000000004
2023-04-27 16:00:00,-1.0,1.7705000000000042
2023-04-27 16:15:00,-1.0,0.7705000000000042
2023-04-27 17:00:00,-1.0,-0.22949999999999582
2023-04-28 13:30:00,1.1407000000000005,0.9112000000000047
2023-04-28 15:30:00,-1.0,-0.08879999999999533
2023-04-28 15:45:00,1.6829999999999998,1.5942000000000045
2023-04-28 16:00:00,1.6643,3.258500000000004
2023-04-28 16:00:00,1.2342,4.492700000000005
2023-04-28 16:00:00,1.2342,5.726900000000004
2023-04-28 16:15:00,1.2903,7.017200000000004
2023-04-29 08:30:00,1.5334,8.550600000000005
2023-04-29 10:30:00,-1.0,7.550600000000005
2023-04-29 10:30:00,0.70125,8.251850000000005
2023-04-29 10:30:00,0.3739999999999999,8.625850000000005
2023-04-29 10:30:00,-1.0,7.625850000000005
2023-04-29 11:00:00,0.3552999999999999,7.981150000000005
2023-04-29 11:00:00,-1.0,6.981150000000005
2023-04-29 11:00:00,-1.0,5.981150000000005
2023-04-29 11:00:00,0.6077499999999999,6.588900000000005
2023-04-29 11:00:00,0.7293000000000001,7.318200000000005
2023-04-29 11:00:00,-1.0,6.318200000000005
2023-04-29 11:15:00,-1.0,5.318200000000005
2023-04-29 11:30:00,0.2431,5.5613000000000055
2023-04-29 12:00:00,0.2805000000000001,5.841800000000005
2023-04-29 13:00:00,-1.0,4.841800000000005
2023-04-29 13:30:00,1.3838,6.225600000000005
2023-04-29 13:30:00,0.1402499999999999,6.365850000000005
2023-04-29 13:30:00,0.86955,7.235400000000006
2023-04-29 15:45:00,1.309,8.544400000000005
2023-04-29 16:00:00,0.3926999999999999,8.937100000000004
2023-04-29 16:30:00,0.2805000000000001,9.217600000000004
2023-04-29 16:30:00,1.1033000000000002,10.320900000000005
2023-04-29 18:30:00,-1.0,9.320900000000005
2023-04-30 07:30:00,0.83215,10.153050000000006
2023-04-30 08:00:00,-1.0,9.153050000000006
2023-04-30 08:00:00,2.1972500000000004,11.350300000000006
2023-04-30 09:00:00,-1.0,10.350300000000006
2023-04-30 09:30:00,1.0846000000000002,11.434900000000006
2023-04-30 10:00:00,1.4399000000000002,12.874800000000006
2023-04-30 10:00:00,-1.0,11.874800000000006
2023-04-30 10:00:00,0.7480000000000001,12.622800000000005
2023-04-30 10:00:00,0.2618,12.884600000000004
2023-04-30 10:00:00,0.2431,13.127700000000004
2023-04-30 10:00:00,-1.0,12.127700000000004
2023-04-30 10:00:00,-1.0,11.127700000000004
2023-04-30 10:00:00,1.0472,12.174900000000004
2023-04-30 10:30:00,0.1028500000000001,12.277750000000005
2023-04-30 11:15:00,0.6358,12.913550000000004
2023-04-30 12:05:00,-1.0,11.913550000000004
2023-04-30 12:30:00,0.5423000000000001,12.455850000000005
2023-04-30 13:00:00,0.3459500000000001,12.801800000000005
2023-04-30 14:00:00,0.21505,13.016850000000005
2023-04-30 15:45:00,0.2992000000000001,13.316050000000006
2023-04-30 15:45:00,-1.0,12.316050000000006
2023-04-30 16:00:00,0.66385,12.979900000000006
2023-04-30 16:30:00,0.2244,13.204300000000005
2023-04-30 18:10:00,0.7854000000000001,13.989700000000006
kupeojn6

kupeojn61#

正如在注解中提到的,您希望每个唯一的日期和时间条目的间距相等。要做到这一点,您需要将日期保持为对象(如代码中所示),使用定位器和格式化程序。发布,用你想要的格式的新日期替换x-标签。这将覆盖日期显示你需要什么。在ax.xaxis.set_major_formatter(formatter)行后面添加以下代码...请注意,这将给予一个警告,因为你正在使用格式化程序,然后覆盖,但应该绘制你需要的。

## Get the labels plotted
    labels = [item.get_text() for item in ax.get_xticklabels()]
    import pandas
    ## Get a list of dates (count of labels) between min and max
    mydates = pandas.date_range(df_chart.open_local_data.min(),df_chart.open_local_data.max(),periods=len(labels))
    ## Convert to text - you can change the way you want to display
    mydates=mydates.strftime('%m/%d/%Y')
    ax.set_xticklabels(mydates)  ## Overwrite labels

剧情

相关问题