gpt4 book ai didi

python - Pyplot 上的多个 x 标签

转载 作者:行者123 更新时间:2023-12-01 08:52:04 25 4
gpt4 key购买 nike

下面是我的折线图代码。我想要在当前标签下再添加一个 x 标签(这样我就可以显示一周中的几天)。

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns;sns.set()

sns.set()

data = pd.read_csv("123.csv")

data['DAY']=["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]

plt.figure(figsize=(15,8))
plt.plot('DAY','SWST',data=data,linewidth=2,color="k")
plt.plot('DAY','WMID',data=data,linewidth=2,color="m")

plt.xlabel('DAY', fontsize=20)
plt.ylabel('VOLUME', fontsize=20)
plt.legend()

Line Graph

编辑:遵循文档后,我有两个问题。刻度从 31 变为 16,并且星期几与天数不一致。

data['DAY']=["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]

tick_labels=['1','\n\nThu','2','\n\nFri','3','\n\nSat','4','\n\nSun','5','\n\nMon','6','\n\nTue','7','\n\nWed','8','\n\nThu','9','\n\nFri','10','\n\nSat','11','\n\nSun','12','\n\nMon','13','\n\nTue','14','\n\nWed','15','\n\nThu','16','\n\nFri','17','\n\nSat','18','\n\nSun','19','\n\nMon','20','\n\nTue','21','\n\nWed','22','\n\nThu','23','\n\nFri','24','\n\nSat','25','\n\nSun','26','\n\nMon','27','\n\nTue','28','\n\nWed','29','\n\nThu','30','\n\nFri','31','\n\nSat']
tick_locations = np.arange(31)

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

plt.xticks(tick_locations, tick_labels)
plt.plot('DAY','SWST',data=data,linewidth=2,color="k")
plt.plot('DAY','WMID',data=data,linewidth=2,color="m")

plt.xlabel('DAY', fontsize=20)
plt.ylabel('VOLUME', fontsize=20)
plt.legend()

plt.show()

Line graph2

最佳答案

您正在寻找的 pyplot 函数是 plt.xticks() 。这本质上是 ax.set_xticks() 的组合。和ax.set_xticklabels()

来自documentation :

Parameters:

ticks : array_like

A list of positions at which ticks should be placed. You can pass an empty list to disable xticks.

labels:

array_like, optional A list of explicit labels to place at the given locs.

您可能需要类似下面的代码。请注意,您可能应该显式设置刻度位置以及标签,以避免将标签设置在错误的位置:

tick_labels = ['1','\n\nThu','2',..., '31','\n\nSat')
plt.xticks(tick_locations, tick_labels)

请注意,面向对象的 API(即使用 ax. )允许更多可定制的绘图。

更新

编辑后,我看到您想要转到下面的标签是同一列表的一部分。因此你的标签列表实际上有 62 的长度。所以你需要将列表中的每 2 个元素连接在一起:

tick_labels=['1','\n\nThu','2','\n\nFri','3','\n\nSat','4','\n\nSun','5','\n\nMon','6','\n\nTue','7','\n\nWed','8',
'\n\nThu','9','\n\nFri','10','\n\nSat','11','\n\nSun','12','\n\nMon','13','\n\nTue','14','\n\nWed','15',
'\n\nThu','16','\n\nFri','17','\n\nSat','18','\n\nSun','19','\n\nMon','20','\n\nTue','21','\n\nWed','22',
'\n\nThu','23','\n\nFri','24','\n\nSat','25','\n\nSun','26','\n\nMon','27','\n\nTue','28','\n\nWed','29',
'\n\nThu','30','\n\nFri','31','\n\nSat']

tick_locations = np.arange(31)

new_labels = [ ''.join(x) for x in zip(tick_labels[0::2], tick_labels[1::2]) ]
plt.figure(figsize=(15, 8))

plt.xticks(tick_locations, new_labels)

plt.show()

enter image description here

关于python - Pyplot 上的多个 x 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53043732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com