如何在顶部的 matplotlib 3D 条形图中放置文本
how to put text in matplotlib 3D bar graph at the top
我需要为我的大学项目绘制一个 3D 条形图,条形图正在绘制,但我希望在每个条形图上显示文本以显示其 z 轴的值。
这是显示二维图形的代码片段:
source code link
#!/usr/bin/env python
# a bar plot with errorbars
import numpy as np
import matplotlib.pyplot as plt
N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)
womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)
# add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )
ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
plt.show()
3D 条形图的代码是 source code for 3d bar-graph
在 2D 版本中,autolabel 函数使用 get_height()、get_x() 和 get_width() 来动态计算它提供的 3D 版本不支持的高度:
- 找不到函数的 patch3d 错误
是否有任何变通方法可以在二维图形中显示文本。
您可以直接从数据中提取条形坐标并使用matplotlib.pyplot。text, as pointed out by igavriil。对于您提供的示例,它看起来像这样:
# Note that z and y-axis are swapped in the plot.
for x,y in zip(xs,ys):
ax.text(x,z,y,'%2.2f'%y, horizontalalignment='left', verticalalignment='bottom')
谢谢igavriil that link helped and also thanks hr tur tur
我不知道那个参数'%2.2f'%y
它是如何工作的,可能有更清晰的方法来绘制它,但我是这样绘制的:
def autolabel():
""" attach some text labels"""
for i in range(0, n):
ax.text(int(x[i])+1, y[i]-2, z[i]+0.05, str(y[i])[0:4], color='#660066', backgroundcolor= '#c187e1', weight= 'bold', rotation='vertical', fontsize=10, horizontalalignment='left', verticalalignment='bottom')
Link:
我需要为我的大学项目绘制一个 3D 条形图,条形图正在绘制,但我希望在每个条形图上显示文本以显示其 z 轴的值。
这是显示二维图形的代码片段: source code link
#!/usr/bin/env python
# a bar plot with errorbars
import numpy as np
import matplotlib.pyplot as plt
N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)
womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)
# add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )
ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
plt.show()
3D 条形图的代码是 source code for 3d bar-graph
在 2D 版本中,autolabel 函数使用 get_height()、get_x() 和 get_width() 来动态计算它提供的 3D 版本不支持的高度:
- 找不到函数的 patch3d 错误
是否有任何变通方法可以在二维图形中显示文本。
您可以直接从数据中提取条形坐标并使用matplotlib.pyplot。text, as pointed out by igavriil。对于您提供的示例,它看起来像这样:
# Note that z and y-axis are swapped in the plot.
for x,y in zip(xs,ys):
ax.text(x,z,y,'%2.2f'%y, horizontalalignment='left', verticalalignment='bottom')
谢谢igavriil that link helped and also thanks hr tur tur
我不知道那个参数'%2.2f'%y
它是如何工作的,可能有更清晰的方法来绘制它,但我是这样绘制的:
def autolabel():
""" attach some text labels"""
for i in range(0, n):
ax.text(int(x[i])+1, y[i]-2, z[i]+0.05, str(y[i])[0:4], color='#660066', backgroundcolor= '#c187e1', weight= 'bold', rotation='vertical', fontsize=10, horizontalalignment='left', verticalalignment='bottom')
Link: