Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用pandas和xlsxwriter库进行群集图表调整_Python 3.x_Xlsxwriter - Fatal编程技术网

Python 3.x 使用pandas和xlsxwriter库进行群集图表调整

Python 3.x 使用pandas和xlsxwriter库进行群集图表调整,python-3.x,xlsxwriter,Python 3.x,Xlsxwriter,我完成了以下代码: import pandas as pd from vincent.colors import brews # Some sample data to plot. farm_1 = {'April': 7, 'July': 98, 'June': 124, 'May': 47} farm_2 = {'April': 7, 'July': 4, 'June': 34, 'May': 45} farm_3 = {'April': 4, 'July': 5, 'June':

我完成了以下代码:

    import pandas as pd
from vincent.colors import brews

# Some sample data to plot.
farm_1 = {'April': 7, 'July': 98, 'June': 124, 'May': 47}
farm_2 = {'April': 7, 'July': 4, 'June': 34, 'May': 45}
farm_3 = {'April': 4, 'July': 5, 'June': 6, 'May': 12}

data  = [farm_1, farm_2, farm_3]
index = ['Farm 1', 'Farm 2', 'Farm 3']

# Create a Pandas dataframe from the data.
df = pd.DataFrame(data, index=index)

# Create a Pandas Excel writer using XlsxWriter as the engine.
sheet_name = 'Sheet1'
writer     = pd.ExcelWriter('pandas_chart_columns.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name=sheet_name)

# Access the XlsxWriter workbook and worksheet objects from the dataframe.
workbook  = writer.book
worksheet = writer.sheets[sheet_name]

# Create a chart object.
chart = workbook.add_chart({'type': 'column'})

# Some alternative colors for the chart.
colors = ['#E41A1C', '#377EB8', '#4DAF4A', '#984EA3', '#FF7F00']

# Configure the series of the chart from the dataframe data.
for col_num in range(1, len(farm_1) + 1):
    chart.add_series({
        'name':       ['Sheet1', 0, col_num],
        'categories': ['Sheet1', 1, 0, 4, 0],
        'values':     ['Sheet1', 1, col_num, 4, col_num],
        'fill':       {'color':  colors[col_num - 1]},
        'overlap':    -10,
    })

# Configure the chart axes.
chart.set_x_axis({'name': 'Total Produce'})
chart.set_y_axis({'name': 'Farms', 'major_gridlines': {'visible': False}})

# Insert the chart into the worksheet.
worksheet.insert_chart('H2', chart)

# Close the Pandas Excel writer and output the Excel file.
writer.save()
它将数据输出为:

我希望它看起来像用绿色突出显示的那个

我尝试更改图表。添加_系列,但没有成功

我是熊猫和xlsxwriter图书馆的新手,仍在努力学习

非常感谢您的指点和帮助

问候,


John

为了使解决方案更简单,我将颜色保留为excel中给定的默认颜色。除此之外,我还制作了您链接中提供的所需图表

我在chart.add_系列中使用了XlsxWriter实用程序模块(link)中的xl_rowcol_to_cell()函数

完整代码如下:

import pandas as pd
from xlsxwriter.utility import xl_rowcol_to_cell

# Some sample data to plot.
farm_1 = {'April': 7, 'July': 98, 'June': 124, 'May': 47}
farm_2 = {'April': 7, 'July': 4, 'June': 34, 'May': 45}
farm_3 = {'April': 4, 'July': 5, 'June': 6, 'May': 12}

data  = [farm_1, farm_2, farm_3]
index = ['Farm 1', 'Farm 2', 'Farm 3']

# Create a Pandas dataframe from the data.
df = pd.DataFrame(data, index=index)

sheet_name = 'Sheet1'
writer     = pd.ExcelWriter('pandas_chart_columns.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name=sheet_name)

# Access the XlsxWriter workbook and worksheet objects from the dataframe.
workbook  = writer.book
worksheet = writer.sheets[sheet_name]

# Create a chart object.
chart = workbook.add_chart({'type': 'column'})

for col_num in range (1, 4):
    cell_1 = xl_rowcol_to_cell(col_num, 0)
    cell_2 = xl_rowcol_to_cell(col_num, 1)
    cell_3 = xl_rowcol_to_cell(col_num, 4)  
    chart.add_series({
        'categories': "='Sheet1'!$B$1:$E$1",
        'name': "='Sheet1'!%s" % (cell_1),  
        'values': "='Sheet1'!%s:%s" % (cell_2, cell_3),
        'overlap':    -10,
    })

chart.set_legend({
    'position': 'bottom',
})  

# Configure the chart axes.
chart.set_x_axis({'name': 'Total Produce'})
chart.set_y_axis({'name': 'Farms',})

# Insert the chart into the worksheet.
worksheet.insert_chart('H2', chart)

writer.save()

您只需要执行与Excel中相同的操作,即切换数据源中的类别和值。像这样:

import pandas as pd

# Some sample data to plot.
farm_1 = {'April': 7, 'July': 98, 'June': 124, 'May': 47}
farm_2 = {'April': 7, 'July': 4, 'June': 34, 'May': 45}
farm_3 = {'April': 4, 'July': 5, 'June': 6, 'May': 12}

data  = [farm_1, farm_2, farm_3]
index = ['Farm 1', 'Farm 2', 'Farm 3']

# Create a Pandas dataframe from the data.
df = pd.DataFrame(data, index=index)

# Create a Pandas Excel writer using XlsxWriter as the engine.
sheet_name = 'Sheet1'
writer     = pd.ExcelWriter('pandas_chart_columns.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name=sheet_name)

# Access the XlsxWriter workbook and worksheet objects from the dataframe.
workbook  = writer.book
worksheet = writer.sheets[sheet_name]

# Create a chart object.
chart = workbook.add_chart({'type': 'column'})

# Configure the series of the chart from the dataframe data.
min_col = 1  # Start from Col B.
max_col = min_col + len(farm_1) -1
name_col = 0
series_row = 0

for row_num in range(1, len(data) + 1):
    chart.add_series({
        'name':       ['Sheet1', row_num, name_col],
        'categories': ['Sheet1', series_row, min_col, series_row, max_col],
        'values':     ['Sheet1', row_num, min_col, row_num, max_col],
    })

# Configure some other chart setting to get the desired output.
chart.set_legend({'position': 'bottom'})
chart.set_y_axis({'major_gridlines': {'visible': False}})

# Insert the chart into the worksheet.
worksheet.insert_chart('G2', chart)

# Close the Pandas Excel writer and output the Excel file.
writer.save()
这将提供所需的输出:


注意,我已将类别和值范围转换为变量,因此如果您的数据集不同,图表将进行调整。

谢谢,这正是我需要的:)