我如何将传奇放在bargraph的外面而不会切断
我尝试关注 如何将传奇放在情节外 但是bargraphs似乎与ax = fig.subplots无法使用。
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1,25)
fig = plt.figure()
legend_array = ["one","two"]
hours_count = [0, 4, 5, 4, 3, 2, 1, 4, 6, 4, 3, 2, 1, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0]
plt.bar(x - 0.5, hours_count, 0.5)
plt.bar(x, hours_count, 0.5)
plt.xlabel("count")
plt.ylabel("high priority")
plt.xticks(x, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24])
#plt.legend(labels = legend_array)
# box = ax.get_position()
# ax.set_position([box.x0, box.y0, box.width * 0.6, box.height])
plt.legend(labels = legend_array,loc='center left', bbox_to_anchor=(1, 0.5))
plt.title("bargraph of high priority errors")
plt.savefig('test.jpg')
我尝试了另一种方法,但似乎可以进一步构造我的图像
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1,25)
fig = plt.figure()
legend_array = ["one","two"]
ax = fig.add_axes([0,0,1,1])
hours_count = [0, 4, 5, 4, 3, 2, 1, 4, 6, 4, 3, 2, 1, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0]
ax.bar(x - 0.5, hours_count, 0.5)
ax.bar(x, hours_count, 0.5)
plt.xlabel("count")
plt.ylabel("high priority")
plt.xticks(x, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24])
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.6, box.height])
ax.legend(labels = legend_array,loc='center left', bbox_to_anchor=(1, 0.5))
plt.title("bargraph of high priority errors")
plt.savefig('test.jpg')
https://i.sstatic.net/h7vax.jpg“ alt =”图形缺少tick和标题,仅占据一半的空间”>
I tried following
How to put the legend outside the plot
but bargraphs dont seem to work with ax = fig.subplots.
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1,25)
fig = plt.figure()
legend_array = ["one","two"]
hours_count = [0, 4, 5, 4, 3, 2, 1, 4, 6, 4, 3, 2, 1, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0]
plt.bar(x - 0.5, hours_count, 0.5)
plt.bar(x, hours_count, 0.5)
plt.xlabel("count")
plt.ylabel("high priority")
plt.xticks(x, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24])
#plt.legend(labels = legend_array)
# box = ax.get_position()
# ax.set_position([box.x0, box.y0, box.width * 0.6, box.height])
plt.legend(labels = legend_array,loc='center left', bbox_to_anchor=(1, 0.5))
plt.title("bargraph of high priority errors")
plt.savefig('test.jpg')
I tried a different method but it seems to mangle my image even further
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1,25)
fig = plt.figure()
legend_array = ["one","two"]
ax = fig.add_axes([0,0,1,1])
hours_count = [0, 4, 5, 4, 3, 2, 1, 4, 6, 4, 3, 2, 1, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0]
ax.bar(x - 0.5, hours_count, 0.5)
ax.bar(x, hours_count, 0.5)
plt.xlabel("count")
plt.ylabel("high priority")
plt.xticks(x, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24])
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.6, box.height])
ax.legend(labels = legend_array,loc='center left', bbox_to_anchor=(1, 0.5))
plt.title("bargraph of high priority errors")
plt.savefig('test.jpg')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论