我如何将传奇放在bargraph的外面而不会切断

发布于 2025-02-07 10:02:19 字数 2012 浏览 2 评论 0原文

我尝试关注 如何将传奇放在情节外 但是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')

BarchartWithCutOffLegend

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')

Graph missing ticks and title and occupying only half the space

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文