在 matplotlib 图表中添加标签

3

有以下代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

days, impressions = np.loadtxt('results_history.csv', unpack=True, delimiter=',',usecols=(0,1) ,
        converters={ 0: mdates.strpdate2num('%d-%m-%y')})

plt.plot_date(x=days, y=impressions, fmt="r-")
plt.title("Load Testing Results")


#params = {'legend.labelsize': 500,
    #'legend.handletextpad': 1,
    #'legend.handlelength': 2,
    #'legend.loc': 'upper left',
    #'labelspacing':0.25,
    #'legend.linewidth': 50}
#plt.rcParams.update(params)

plt.legend("response times")

plt.ylabel("Date")
plt.grid(True)
plt.show()

图表已经生成,但我不知道如何添加一些xy标签。生成的图表:enter image description here 同时尝试增加图例文本大小,但文本未显示。X轴标签重叠了。CSV文件:
01-05-14, 55494, Build 1
10-05-14, 55000, Build 2
15-05-14, 55500, Build 3
20-05-14, 57482, Build 4
25-05-14, 58741, Build 5

我怎样才能从CSV文件中添加xytext,还可以更改图例和X轴的格式?


你实际上在一个问题中提出了三个问题。 - Ffisegydd
1个回答

5
你需要使用注释 annotate,例如:
plt.annotate('some text',xy=(days[0],impressions[0]))

要调整x轴文本,您可以添加:

fig=plt.figure() # below the import statements
...
fig.autofmt_xdate() # after plotting

如果要更改图例文本,请在绘图功能中使用label参数:

plt.plot_date(x=days, y=impressions, fmt="r-",label="response times")

为了增加图例字体大小,请按照以下步骤操作:
plt.legend(fontsize='x-large')

这会返回AttributeError: 'str' object has no attribute 'toordinal'。它也未能回答OP的其他问题部分。 - Ffisegydd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接