标签名称显示方框
时间: 2024-02-17 07:03:13 浏览: 148
如果想要在行列索引名称的周围显示方框,可以使用matplotlib中的Table函数进行实现。具体步骤如下:
1. 首先,读取数据表并将FactoryName和JiJXH作为行索引,Shi作为列索引,可以使用pandas中的pivot函数实现:
```
import pandas as pd
# 读取数据表
df = pd.read_csv('data.csv')
# 将FactoryName和JiJXH作为行索引,Shi作为列索引
df_pivot = df.pivot(index=['FactoryName', 'JiJXH'], columns='Shi', values='sale_avg')
```
2. 接下来,根据每行sale_avg的值生成对应的色阶,可以使用matplotlib中的colormap函数实现:
```
import matplotlib.pyplot as plt
# 生成色阶
colormap = plt.cm.Reds
```
3. 然后,使用matplotlib中的Table函数创建一个新的表格,并设置行列索引名称和单元格颜色:
```
# 创建一个新的表格
table = plt.table(cellText=df_pivot.values, colLabels=df_pivot.columns, rowLabels=df_pivot.index, loc='center', cellColours=plt.cm.Reds(df_pivot))
# 隐藏表格边框
table.auto_set_font_size(False)
table.set_fontsize(14)
for i in range(len(df_pivot.columns)):
for j in range(len(df_pivot.index)):
table._cells[(j+1, i)].set_edgecolor('white')
table._cells[(j+1, i)].set_text_props(weight='bold')
```
在这里,`plt.table()`函数用于创建一个新的表格,其中`cellText`参数为单元格中的文本,`colLabels`参数为列索引名称,`rowLabels`参数为行索引名称,`loc`参数为表格的位置。`cellColours`参数用于设置单元格的颜色,这里使用了与`df_pivot`数据表中sale_avg列的颜色相对应的Reds色阶。然后,使用`set_edgecolor()`函数和`set_text_props()`函数设置单元格的边框颜色和文本样式,这里将边框颜色设置为白色,将文本样式设置为粗体。
4. 最后,使用matplotlib中的subplots_adjust函数调整表格和图形的位置和大小,并显示图形:
```
# 调整表格和图形的位置和大小
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.axis('off')
# 显示图形
plt.show()
```
完整代码如下:
```
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据表
df = pd.read_csv('data.csv')
# 将FactoryName和JiJXH作为行索引,Shi作为列索引
df_pivot = df.pivot(index=['FactoryName', 'JiJXH'], columns='Shi', values='sale_avg')
# 生成色阶
colormap = plt.cm.Reds
# 创建一个新的表格
table = plt.table(cellText=df_pivot.values, colLabels=df_pivot.columns, rowLabels=df_pivot.index, loc='center', cellColours=plt.cm.Reds(df_pivot))
# 隐藏表格边框
table.auto_set_font_size(False)
table.set_fontsize(14)
for i in range(len(df_pivot.columns)):
for j in range(len(df_pivot.index)):
table._cells[(j+1, i)].set_edgecolor('white')
table._cells[(j+1, i)].set_text_props(weight='bold')
# 调整表格和图形的位置和大小
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.axis('off')
# 显示图形
plt.show()
```
在这里,使用`auto_set_font_size()`函数和`set_fontsize()`函数设置表格字体的大小。然后,使用`subplots_adjust()`函数调整表格和图形的位置和大小。最后,使用`axis()`函数隐藏坐标轴。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)