python - Python:调整雷达图上的 x 轴标签
问题描述
我已经绘制了一个雷达图。但是 x 标签与图表相交。如何调整它的放置使其不与网格发生冲突?
# Libraries
import matplotlib.pyplot as plt
import pandas as pd
from math import pi
# Set data
#9,443 8,951 3,162 1,744 486 23,786
# 16,019 24,004 8,049 8,729 415 57,218
df = pd.DataFrame({
'group': ['1996','2016'],
'Natural Gas': [2079, 12304],
'Petroleum Products': [17203, 30348],
'Coal & Coke': [727, 1785],
'Biodiesel': [0, 389],
'Electricity': [3777, 12392]
})
# ------- PART 1: Create background
# number of variable
categories=list(df)[1:]
N = len(categories)
# What will be the angle of each axis in the plot? (we divide the plot / number of variable)
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:1]
# Initialise the spider plot
ax = plt.subplot(111, polar=True)
# If you want the first axis to be on top:
ax.set_theta_offset(pi / 2)
ax.set_theta_direction(-1)
# Draw one axe per variable + add labels labels yet
plt.xticks(angles[:-1], categories)
# Draw ylabels
ax.set_title('Energy Consumption by Fuel Type (Mtoe)', position=(0.5, 1.1), ha='center', fontweight="bold")
ax.set_rlabel_position(0)
plt.yticks([10000,20000,30000,40000], ["10","20","30","40"], color="grey", size=8, fontweight="bold")
plt.ylim(0,40000)
# ------- PART 2: Add plots
# Plot each individual = each line of the data
# I don't do a loop, because plotting more than 3 groups makes the chart unreadable
# Ind1
values=df.loc[0].drop('group').values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, linewidth=1, linestyle='solid', label="1996, 23.786 Mtoe", color="green")
ax.fill(angles, values, 'green', alpha=0.4)
# Ind2
values=df.loc[1].drop('group').values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, linewidth=1, linestyle='solid', label="2016, 57.218 Mtoe",color="purple")
ax.fill(angles, values, 'purple', alpha=0.2)
# Add legend
plt.legend(loc='upper right', bbox_to_anchor=(0.5, 0.001))
输出图表如下:
请指导。Tq。
解决方案
编辑轴和标签之间的填充将标签移开,但看起来仍然不是最好的。
ax.tick_params(axis='both', which='major', pad=25)
推荐阅读
- javascript - 如何旋转图像然后保存新的旋转图像?
- node.js - 使用 Lambda 定期清除 Redis 缓存
- android - 使用 WorkManager 的定期日常工作请求
- javascript - GDrive 分段上传 jQuery
- css - 在移动设备上使用浏览器默认实现 CSS 选择
- python-3.x - 需要制作具有年份和星期组合的字符串变量到日期时间格式
- php - html/ php 表没有填充只是显示空白页
- c# - 在简单集合和列表中获取和设置值 - C#
- angular - 你如何让滚动条在移动设备中持续存在?
- moodle - 将moodle登录限制为特定的URL