matplotlib text加标签功能问题求助
import pandas as pdimport matplotlib.pyplot as plt
df=pd.read_excel('2.xlsx').convert_dtypes()
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
df['date'] = df['date'].dt.date
print(df.head())
df.plot.bar(x='date',
y='时长')
for i,j in enumerate(zip(df['date'],df['时长'])):
plt.text(i,j,j)
plt.show()
想在每个柱子上加上时长,具体见附件的图片
上述代码报错如下:
matplotlib.units.ConversionError: Failed to convert value(s) to axis units: (datetime.date(2020, 7, 1), 2)
求助:
应该如何写,才能加上标签,报错的问题如何解决?
plt.text(x,y,text)第一个参数和第二个参数分别是x、y 轴坐标,第三个是注释文本内容
你这个报错好像是因为直接用日期当成 x 轴,是不允许的,只能弄成 range() 之类的一个具体的坐标
matplotlib 柱状图问题
https://fishc.com.cn/thread-174641-1-1.html
(出处: 鱼C论坛)
那接受什么格式呢?字符串?或者数值?还是只要放在列表中就可以呢? Twilight6 发表于 2020-8-3 13:43
plt.text(x,y,text)第一个参数和第二个参数分别是x、y 轴坐标,第三个是注释文本内容
你这个报错好 ...
只要是列表里就可以吗?还是要求列表里的内容格式有要求? rsj0315 发表于 2020-8-3 14:09
只要是列表里就可以吗?还是要求列表里的内容格式有要求?
没尝试过小数,但是整数肯定可以,你用 plt.xticks 来设置月份信息,然后 x 轴实际刻度用 range 整数吧
Twilight6 发表于 2020-8-3 13:43
plt.text(x,y,text)第一个参数和第二个参数分别是x、y 轴坐标,第三个是注释文本内容
你这个报错好 ...
正解 Tip0 发表于 2020-8-3 15:54
正解
时间和数值是pandas读取的excel的,如何用range转换呢?
枚举的方法不行,是因为dtype的问题?还是什么呢?
搜半天不知道问题在哪里 rsj0315 发表于 2020-8-3 19:13
时间和数值是pandas读取的excel的,如何用range转换呢?
枚举的方法不行,是因为dtype的问题?还是什么 ...
你读取出 date 的长度然后 range(len(date)) 你那个数据的长度用
xlabel =
plt.xticks(rang(len(date)),xlabel)
这样即可啊
Twilight6 发表于 2020-8-5 07:35
你读取出 date 的长度然后 range(len(date)) 你那个数据的长度用
是我理解错了么?
还是你没理解对呢?
现在问题是在柱子的上边加上时长。
for i,j in enumerate(zip(df['date'],df['时长'])):
plt.text(i,j,j)
plt.text这里过不去。
x轴的xticks和xlabel按照你说的设置或者直接从dateframe里拿没问题的啊。 rsj0315 发表于 2020-8-5 20:33
是我理解错了么?
还是你没理解对呢?
现在问题是在柱子的上边加上时长。
我是说你报错大致解决方法啊 , 你帖子不是说了吗? 报错怎么解决,怎么还问起我来了 Twilight6 发表于 2020-8-5 22:37
我是说你报错大致解决方法啊 , 你帖子不是说了吗? 报错怎么解决,怎么还问起我来了
哦哦,了解,我以为答的是怎么把那数值加上去。
我找到方法了,明天有时间把代码放上去。
是借用了下numpy fig = plt.figure()
#use a figure size of (20, 8),bar width of 0.8, 设置图片大小,柱宽
#use color #5cb85c for the Very interested bars, 设置柱子颜色
#color #5bc0de for the Somewhat interested bars,
#color #d9534f for the Not interested bars,
c=topicp.plot(kind='bar', y=['Very interested','Somewhat interested','Not interested'],figsize=(20, 8),width=0.8,
color=['#5cb85c','#5bc0de','#d9534f'],fontsize=14)
#use font size 14 for the bar labels, percentages, and legend, 图例颜色
plt.legend(fontsize=14)
#use font size 16 for the title, and, 标题字号
plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize=16)
plt.yticks([]) # y轴空轴
#display the percentages above the bars as shown above 数据标签列表
x=np.arange(len(topicp.index))
yv=np.array(list(topicp['Very interested']))
ys=np.array(list(topicp['Somewhat interested']))
yn=np.array(list(topicp['Not interested']))
for a,b in zip(x,yv): ##控制标签位置
plt.text(a-0.27,b+0.1,'%.2f'%b,ha = 'center',va = 'bottom',fontsize=14)
for a,b in zip(x,ys):
plt.text(a,b+0.1,'%.2f'%b,ha = 'center',va = 'bottom',fontsize=14)
for a,b in zip(x,yn):
plt.text(a+0.27,b+0.1,'%.2f'%b,ha = 'center',va = 'bottom',fontsize=14)
#remove the left, top, and right borders. 去掉图片边框
c.spines['top'].set_visible(False)
c.spines['right'].set_visible(False)
#c.spines['bottom'].set_visible(False) 保留横坐标边框
c.spines['left'].set_visible(False)
plt.show
https://blog.csdn.net/WallisY/article/details/98482700
用的连接中这个方法,可以实现
页:
[1]