与python、pandas和matplotlib中的透视表相比,子批大小较小
我试图将pivot保存为jpg格式,但帧(图)与pivot表相比非常小,无法在jpg中捕获整个帧与python、pandas和matplotlib中的透视表相比,子批大小较小,python,python-3.x,pandas,matplotlib,size,Python,Python 3.x,Pandas,Matplotlib,Size,我试图将pivot保存为jpg格式,但帧(图)与pivot表相比非常小,无法在jpg中捕获整个帧 数据: 你能告诉我解决这个问题的方法吗 我尝试了几个选项,但仍然没有得到图像中的整个轴,而且数量是十进制的,正如我想要的那样,是整数 figsize=(20,15)或(30,15) plt.savefig('test.png',bbox\u inches='tight',pad\u inches=0.1) 是否有其他方法可以将轴保存为jpg格式,以便获得整个轴。使用此方法将数量设置为整数: d
数据: 你能告诉我解决这个问题的方法吗 我尝试了几个选项,但仍然没有得到图像中的整个轴,而且数量是十进制的,正如我想要的那样,是整数
figsize=(20,15)
或(30,15)
plt.savefig('test.png',bbox\u inches='tight',pad\u inches=0.1)
是否有其他方法可以将轴保存为jpg格式,以便获得整个轴。使用此方法将数量设置为整数:
df=df.pivot_table(df,index=['Manager','Status'],columns=['Product'],aggfunc={'Price':np.sum,'Quantity':len},fill_value=-1,margins=False)
df["Quantity"] = df["Quantity"].astype(int)
df = df.replace(-1,"-")
要将表存储为图像,它的工作方式如下:
fig=plt.figure(figsize=(15,10))
ax=plt.subplot(111,frame_on=False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
tb1=table(ax,df,loc='center',colWidths=[0.15, 0.24, 0.2,0.2,0.25,0.22])
tb1.auto_set_font_size(False)
tb1.set_fontsize(14)
tb1.scale(1,6)
#plt.show()
plt.savefig('test.png', bbox_inches='tight', pad_inches=0.1)
生成的png:
编辑
要使标题和索引加粗并更改文本颜色,请执行以下操作:
from matplotlib import font_manager
fig=plt.figure(figsize=(18,10))
ax=plt.subplot(111,frame_on=False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
tb1=table(ax,df,loc='center',colWidths=[0.15, 0.24, 0.2,0.2,0.25,0.22])
for (row, col), cell in tb1.get_celld().items():
#make bold the column headers and the indexes
if (row == 0) or (col == -1):
cell.set_text_props(fontproperties=font_manager.FontProperties(weight='bold'))
#only if the cell is a column header then change color
if row == 0:
cell.get_text().set_color('red')
tb1.auto_set_font_size(False)
tb1.set_fontsize(14)
tb1.scale(1,6)
plt.show()
这是下面编码的输出,它有标题(价格)和子标题(CPU、维护和软件
但是当我在图形中绘制时,下面的编码标题和子标题被合并到一个单元格中。我们如何避免这种情况呢?
*我希望价格和数量与子标题(CPU、维护和软件)一样位于顶部,如第一幅图所示。
谢谢*在文本中提供数据,而不是按照要求的图像,我已经以文本格式添加了数据。谢谢@DavideBrex,索引和标题可以加粗吗?如果我想添加颜色,我们可以怎么做?请参阅更新的答案,以使索引和标题加粗。对于颜色,您是指单元格的颜色?还是单元格中的文本?为标题的颜色单元格为浅蓝色,如果我想为特定列的特定单元格上色,比如我想为列-(“价格”-“软件”)中的值上色为红色文本.Hi David你能告诉我如何在标题单元格中设置文本的颜色吗?嗨,更改列标题对我来说是可行的,但是列的标题没有用粗体显示,因为这个命令抛出了一个错误“name'font\u manager'未定义”“cell.set\u text\u props(fontproperties=font\u manager.fontproperties(weight='bold')”您好!matplotlib表似乎不可能(请参阅)。或者,您可以使用
df.to_html()
或df.to_latex()
,其中可以使用多索引
fig=plt.figure(figsize=(15,10))
ax=plt.subplot(111,frame_on=False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
tb1=table(ax,df,loc='center',colWidths=[0.15, 0.24, 0.2,0.2,0.25,0.22])
tb1.auto_set_font_size(False)
tb1.set_fontsize(14)
tb1.scale(1,6)
#plt.show()
plt.savefig('test.png', bbox_inches='tight', pad_inches=0.1)
from matplotlib import font_manager
fig=plt.figure(figsize=(18,10))
ax=plt.subplot(111,frame_on=False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
tb1=table(ax,df,loc='center',colWidths=[0.15, 0.24, 0.2,0.2,0.25,0.22])
for (row, col), cell in tb1.get_celld().items():
#make bold the column headers and the indexes
if (row == 0) or (col == -1):
cell.set_text_props(fontproperties=font_manager.FontProperties(weight='bold'))
#only if the cell is a column header then change color
if row == 0:
cell.get_text().set_color('red')
tb1.auto_set_font_size(False)
tb1.set_fontsize(14)
tb1.scale(1,6)
plt.show()
import pandas as pd
from pandas.plotting import table
import numpy as np
import matplotlib.pyplot as plt
df=pd.read_excel('Book1.xlsx')
df
df=df.pivot_table(df,index=['Manager','Status'],columns=['Product'],aggfunc={'Price':np.sum,'Quantity':len},fill_value='-',margins=False)
df
fig=plt.figure(figsize=(15,10))
ax=plt.subplot(111,frame_on=False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
tb1=table(ax,df,loc='center',colWidths=[0.15, 0.24, 0.2,0.2,0.25,0.22])
tb1.set_fontsize(17)
tb1.scale(1,7)
plt.show()