**Title**: Issue with openpyxl version 3.1.4: X and Y axis labels not displaying in Excel chart (It works with the version 3.1.2)

Title: Issue with openpyxl version 3.1.4: X and Y axis labels not displaying in Excel chart

Description:
I encountered an issue with the openpyxl library where values on the X and Y axes are not displayed correctly in an Excel chart. This issue occurs with openpyxl version 3.1.4 but works correctly with version 3.1.2.

Environment:

  • openpyxl version: 3.1.4 (problematic), 3.1.2 (working)
  • Python version: 3.11.9
  • Operating System: [Windows 10 and 11]

Steps to Reproduce:

  1. Create a DataFrame with the following data:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
from openpyxl import Workbook
from openpyxl.chart import LineChart, Reference, Series
from openpyxl.utils.dataframe import dataframe_to_rows
data = {
'Year': ['2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023'],
'serie_1': [1, 2, 3, 4, 5, 6, 7, 8, 9],
'serie_2': [2, 3, 4, 5, 6, 7, 8, 9, 10],
'serie_3': [3, 4, 5, 6, 7, 8, 9, 10, 11]
}
df = pd.DataFrame(data)
# Create a workbook and select the active worksheet
wb = Workbook()
ws = wb.active
# Write the DataFrame to the worksheet
for r in dataframe_to_rows(df, index=False, header=True):
ws.append(r)
# Create a LineChart
chart = LineChart()
chart.title = "Revenue Over Years"
chart.style = 10
chart.x_axis.title = "Year"
chart.y_axis.title = "Revenue"
# Define categories (X axis values) using the first column (Year)
categories = Reference(ws, min_col=1, min_row=2, max_row=ws.max_row)
# Add the series to the chart
for i in range(2, ws.max_column + 1):
values = Reference(ws, min_col=i, min_row=2, max_row=ws.max_row)
series = Series(values, title=ws.cell(row=1, column=i).value)
chart.series.append(series)
chart.set_categories(categories)
# Set the size of the chart in centimeters
chart.width = 15 # Width in cm
chart.height = 10 # Height in cm
# Ensure axis labels and gridlines are displayed
chart.x_axis.majorGridlines = ChartLines()
chart.y_axis.majorGridlines = ChartLines()
chart.x_axis.tickLblPos = 'nextTo'
chart.y_axis.tickLblPos = 'nextTo'
# Position the chart on the sheet
ws.add_chart(chart, "E5") # Position the chart starting from cell E5
# Save the Excel file
wb.save("test_graph.xlsx")
I would expect that the problem will be solved in a future version. I use the older one for the time being.
with version 3.1.4 of openpyxl only the label are displayed but not values (2015,2016,... on x and 1..11 on y)
I would help the other onfronted to it to save some time. Just try the version 3.1.2 to see if it solve your problem.
</code>
<code>import pandas as pd from openpyxl import Workbook from openpyxl.chart import LineChart, Reference, Series from openpyxl.utils.dataframe import dataframe_to_rows data = { 'Year': ['2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023'], 'serie_1': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'serie_2': [2, 3, 4, 5, 6, 7, 8, 9, 10], 'serie_3': [3, 4, 5, 6, 7, 8, 9, 10, 11] } df = pd.DataFrame(data) # Create a workbook and select the active worksheet wb = Workbook() ws = wb.active # Write the DataFrame to the worksheet for r in dataframe_to_rows(df, index=False, header=True): ws.append(r) # Create a LineChart chart = LineChart() chart.title = "Revenue Over Years" chart.style = 10 chart.x_axis.title = "Year" chart.y_axis.title = "Revenue" # Define categories (X axis values) using the first column (Year) categories = Reference(ws, min_col=1, min_row=2, max_row=ws.max_row) # Add the series to the chart for i in range(2, ws.max_column + 1): values = Reference(ws, min_col=i, min_row=2, max_row=ws.max_row) series = Series(values, title=ws.cell(row=1, column=i).value) chart.series.append(series) chart.set_categories(categories) # Set the size of the chart in centimeters chart.width = 15 # Width in cm chart.height = 10 # Height in cm # Ensure axis labels and gridlines are displayed chart.x_axis.majorGridlines = ChartLines() chart.y_axis.majorGridlines = ChartLines() chart.x_axis.tickLblPos = 'nextTo' chart.y_axis.tickLblPos = 'nextTo' # Position the chart on the sheet ws.add_chart(chart, "E5") # Position the chart starting from cell E5 # Save the Excel file wb.save("test_graph.xlsx") I would expect that the problem will be solved in a future version. I use the older one for the time being. with version 3.1.4 of openpyxl only the label are displayed but not values (2015,2016,... on x and 1..11 on y) I would help the other onfronted to it to save some time. Just try the version 3.1.2 to see if it solve your problem. </code>
import pandas as pd
from openpyxl import Workbook
from openpyxl.chart import LineChart, Reference, Series
from openpyxl.utils.dataframe import dataframe_to_rows

data = {
    'Year': ['2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023'],
    'serie_1': [1, 2, 3, 4, 5, 6, 7, 8, 9],
    'serie_2': [2, 3, 4, 5, 6, 7, 8, 9, 10],
    'serie_3': [3, 4, 5, 6, 7, 8, 9, 10, 11]
}
df = pd.DataFrame(data)

# Create a workbook and select the active worksheet
wb = Workbook()
ws = wb.active

# Write the DataFrame to the worksheet
for r in dataframe_to_rows(df, index=False, header=True):
    ws.append(r)

# Create a LineChart
chart = LineChart()
chart.title = "Revenue Over Years"
chart.style = 10
chart.x_axis.title = "Year"
chart.y_axis.title = "Revenue"

# Define categories (X axis values) using the first column (Year)
categories = Reference(ws, min_col=1, min_row=2, max_row=ws.max_row)

# Add the series to the chart
for i in range(2, ws.max_column + 1):
    values = Reference(ws, min_col=i, min_row=2, max_row=ws.max_row)
    series = Series(values, title=ws.cell(row=1, column=i).value)
    chart.series.append(series)

chart.set_categories(categories)

# Set the size of the chart in centimeters
chart.width = 15  # Width in cm
chart.height = 10  # Height in cm

# Ensure axis labels and gridlines are displayed
chart.x_axis.majorGridlines = ChartLines()
chart.y_axis.majorGridlines = ChartLines()
chart.x_axis.tickLblPos = 'nextTo'
chart.y_axis.tickLblPos = 'nextTo'

# Position the chart on the sheet
ws.add_chart(chart, "E5")  # Position the chart starting from cell E5

# Save the Excel file
wb.save("test_graph.xlsx")


I would expect that the problem will be solved in a future version. I use the older one for the time being.

with version 3.1.4 of openpyxl only the label are displayed but not values (2015,2016,... on x and 1..11 on y)

I would help the other onfronted to it to save some time. Just try the version 3.1.2 to see if it solve your problem.

New contributor

user26456091 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật