添加文字后,情节变得很小

发布于 2025-02-01 11:32:09 字数 2471 浏览 3 评论 0 原文

我正在对欧洲电视网进行一些研究,多年来我绘制了一些国家的观点。 我最终将每个国家 /地区缩放,以便每年从0-100缩放得分。

我制作了一系列我使用有用的国家 /地区 。

然后,我使用以下代码来绘制和注释乌克兰:

plt.style.use("default")

# UKRAINE
fig, ax1 = plt.subplots(1, 1, sharey=False, figsize=(20, 10))

country = eurovision.loc[eurovision["Country"] == usefull[0]]
ax1.plot(country["Year"].astype(str), country["Scaled Grand Final Points"], label = "Ukraine", color = "#0057b7")

# ANNOTATING EVENTS
ax1.text(13.5, 50, "                      2020 \n \n No contest because of COVID-19", 
fontsize = 12, bbox=dict(facecolor='#ffd700', alpha=0.7))
ax1.text(13.5, 92, "                WON IN 2022 \n \n   A lot of sympathy votes because \n of the Russian War against Ukraine", 
fontsize = 11, bbox=dict(facecolor='#ffd700', alpha=0.7))
ax1.text(4.8, 92, "                                                WON IN 2016 \n \n                   Their song '1944' received lots of sympathy votes \n The song refers to the mass deportation of Crimean Tatars by Joseph Stalin", 
fontsize = 11, bbox=dict(facecolor='#ffd700', alpha=0.7))
ax1.text(0.05, 1.7, "                             2005 \n \n Low votes from eastern countries because \n    of the pro-European Orange Revolution", 
fontsize = 11, bbox=dict(facecolor='#ffd700', alpha=0.7))
ax1.text(4.5, 4.1, "                     2009 \n \n Low votes from eastern countries due \n          to an energy price conflict", 
fontsize = 11, bbox=dict(facecolor='#ffd700', alpha=0.7))

ax1.set_title("Ukraine")
ax1.set_xlabel("Year")
ax1.set_ylabel("Total final points")
ax1.legend(bbox_to_anchor = (1, 0.1))

这很好,因为它输出了带有正确注释的不错的绘图,所以这很好 https://i.sstatic.net/8bc4b.png

但是,当我尝试做时在西班牙完全相同:

# SPAIN
fig, ax9 = plt.subplots(1, 1, sharey=False, figsize=(20, 10))

country = eurovision.loc[eurovision["Country"] == usefull[8]]
ax9.plot(country["Year"], country["Scaled Grand Final Points"], label = "Spain", color = "#FABD00")

ax9.text(1.3, 21, "                                       1960 \n \n Random event", 
fontsize = 11, bbox=dict(facecolor='#FFF08', alpha=0.7))

ax9.set_title("Spain")
ax9.set_xlabel("Year")
ax9.set_ylabel("Total final points")
ax9.legend()

我将输出一个带有小注释框的超级小块。 https://i.sstatic.net/nrpez.png

我真的非常感谢大家帮助我找到修复程序!

I'm doing some research on Eurovision and I plotted some countries' points throughout the years.
I min-max scaled every country so that points are scaled from 0 - 100 for every year.

I made an array of the countries I use usefull = ['Ukraine', 'United Kingdom', 'Russia', 'Georgia', 'France', 'Armenia', 'Azerbaijan', 'Israel', 'Spain', 'Austria', 'Turkey']

I then used the following code to plot and annotate on Ukraine:

plt.style.use("default")

# UKRAINE
fig, ax1 = plt.subplots(1, 1, sharey=False, figsize=(20, 10))

country = eurovision.loc[eurovision["Country"] == usefull[0]]
ax1.plot(country["Year"].astype(str), country["Scaled Grand Final Points"], label = "Ukraine", color = "#0057b7")

# ANNOTATING EVENTS
ax1.text(13.5, 50, "                      2020 \n \n No contest because of COVID-19", 
fontsize = 12, bbox=dict(facecolor='#ffd700', alpha=0.7))
ax1.text(13.5, 92, "                WON IN 2022 \n \n   A lot of sympathy votes because \n of the Russian War against Ukraine", 
fontsize = 11, bbox=dict(facecolor='#ffd700', alpha=0.7))
ax1.text(4.8, 92, "                                                WON IN 2016 \n \n                   Their song '1944' received lots of sympathy votes \n The song refers to the mass deportation of Crimean Tatars by Joseph Stalin", 
fontsize = 11, bbox=dict(facecolor='#ffd700', alpha=0.7))
ax1.text(0.05, 1.7, "                             2005 \n \n Low votes from eastern countries because \n    of the pro-European Orange Revolution", 
fontsize = 11, bbox=dict(facecolor='#ffd700', alpha=0.7))
ax1.text(4.5, 4.1, "                     2009 \n \n Low votes from eastern countries due \n          to an energy price conflict", 
fontsize = 11, bbox=dict(facecolor='#ffd700', alpha=0.7))

ax1.set_title("Ukraine")
ax1.set_xlabel("Year")
ax1.set_ylabel("Total final points")
ax1.legend(bbox_to_anchor = (1, 0.1))

This worked out fine as it outputted a nice plot with the correct annotations.
https://i.sstatic.net/8bc4B.png

However when I tried to do the exact same thing on Spain:

# SPAIN
fig, ax9 = plt.subplots(1, 1, sharey=False, figsize=(20, 10))

country = eurovision.loc[eurovision["Country"] == usefull[8]]
ax9.plot(country["Year"], country["Scaled Grand Final Points"], label = "Spain", color = "#FABD00")

ax9.text(1.3, 21, "                                       1960 \n \n Random event", 
fontsize = 11, bbox=dict(facecolor='#FFF08', alpha=0.7))

ax9.set_title("Spain")
ax9.set_xlabel("Year")
ax9.set_ylabel("Total final points")
ax9.legend()

I get outputted a super small plot with a small annotation box.
https://i.sstatic.net/nrpeZ.png

I really thank you guys in advance for helping me finding the fix!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2025-02-08 11:32:10

使用您的数据,我获得了西班牙的正常尺寸图。我正在使用 python 3.8.9 matplotlib版本3.5.1 在Mac darwin内核版本20.6.0

/I.SSTATIC.NET/ZK2TE.JPG“ rel =” nofollow noreferrer 乌克兰与西班牙的代码中有一个区别,这使我的第一次尝试中没有出现文本和边界框。您在代码中缺少 astype(str) ax99.plot

ax9.plot(country["Year"].astype(str), country["Scaled Grand Final Points"], label = "Spain", color = "#FABD00")

# rotates the x-axis labels by 45 degrees
plt.setp(ax9.get_xticklabels(), rotation=45, horizontalalignment='right', fontsize='x-small')

# sets alternate labels to off
for n, label in enumerate(ax9.xaxis.get_ticklabels()):
   if n % 2 !=0:
      label.set_visible(False)

一旦我修复了这条线,我就将文字在剧情中得到了,就像乌克兰一样。

Using your data, I get a normal size plot for Spain. I am using python 3.8.9 and matplotlib version 3.5.1 on Mac Darwin Kernel Version 20.6.0

Spain Plot

I notice one difference in your code for Ukraine vs Spain which made the text and bounding box not appear in my first attempt. You are missing astype(str) in your code for ax9.plot

ax9.plot(country["Year"].astype(str), country["Scaled Grand Final Points"], label = "Spain", color = "#FABD00")

# rotates the x-axis labels by 45 degrees
plt.setp(ax9.get_xticklabels(), rotation=45, horizontalalignment='right', fontsize='x-small')

# sets alternate labels to off
for n, label in enumerate(ax9.xaxis.get_ticklabels()):
   if n % 2 !=0:
      label.set_visible(False)

Once I fixed this line, I got the text in the plot - same like Ukraine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文