I want to change the format (date and time display) of the legend in chronological order.

Asked 2 months ago, Updated 2 months ago, 396 views

We generated a line graph by performing the following:
If you want the time in the legend to format YYYY-MM-DD (date only, no time), where should I fix it?

for i in range(0,10):
    plt.legend(loc='upper left',bbox_to_anchor=(1,1)))
    plt.ticklabel_format (style='plain', axis='y')
    plt.savefig(fig_name, bbox_inches="tight", pad_inches=0.25) 

Enter a description of the image here

python pandas

2023-01-13 16:58

1 Answers

Please refer to the comments to your question and use the similar data you have

#plt.legend(loc='upper left', bbox_to_anchor=(1,1)))
           loc='upper left', bbox_to_anchor=(1,1))

I tried and got the desired result, but according to matplotlib.pyplot.legend, this format seems to be deprecated.Here is an example of a description to take out and process the label, and the result of the drawing (of similar data).

lbls = [s.split()[0] for s in lbls] #YYYY-MM-DD
# lbls=[s.split()[0][5:] for in lbls]#MM-DD
ax.legend(handles=hndls, labels=lbls,
          loc='upper left', bbox_to_anchor=(1,1))
ax.ticklabel_format (style='plain', axis='y')
plt.savefig(fig_name, bbox_inches="tight", pad_inches=0.25)

drawing results

2023-01-13 17:30

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.