Python Forum
How to change font size of chart title and axis title ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change font size of chart title and axis title ?
#1
Hi, experts

I want to change font size of chart title and axis(x,y) title.
Can you help me ?

Thanks in advance

######################### BOF #######################################
### Python 3.6.3/ openpyxl 2.4.9
from datetime import date
from openpyxl import Workbook
from openpyxl.chart import (
LineChart,
Reference,
)
from openpyxl.chart.axis import DateAxis

wb = Workbook()
ws = wb.active

rows = [
['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
[date(2015,9, 1), 40, 30, 25],
[date(2015,9, 2), 40, 25, 30],
[date(2015,9, 3), 50, 30, 45],
[date(2015,9, 4), 30, 25, 40],
[date(2015,9, 5), 25, 35, 30],
[date(2015,9, 6), 20, 40, 35],
]

for row in rows:
ws.append(row)

c1 = LineChart()
c1.title = "Line Chart"
c1.style = 13
c1.y_axis.title = 'Size'
c1.x_axis.title = 'Test Number'

data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
c1.add_data(data, titles_from_data=True)

# Style the lines
s1 = c1.series[0]
s1.marker.symbol = "triangle"
s1.marker.graphicalProperties.solidFill = "FF0000" # Marker filling
s1.marker.graphicalProperties.line.solidFill = "FF0000" # Marker outline

s1.graphicalProperties.line.noFill = True

s2 = c1.series[1]
s2.graphicalProperties.line.solidFill = "00AAAA"
s2.graphicalProperties.line.dashStyle = "sysDot"
s2.graphicalProperties.line.width = 100050 # width in EMUs

s2 = c1.series[2]
s2.smooth = True # Make the line smooth
ws.add_chart(c1, "A10")
wb.save("line.xlsx")
Reply
#2
I rewrite my post with bbcode.
I am sorry for the inconvenience to all of you.

### Python 3.6.3/ openpyxl 2.4.9
from datetime import date
from openpyxl import Workbook
from openpyxl.chart import (
LineChart,
Reference,
)
from openpyxl.chart.axis import DateAxis

wb = Workbook()
ws = wb.active

rows = [
['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
[date(2015,9, 1), 40, 30, 25],
[date(2015,9, 2), 40, 25, 30],
[date(2015,9, 3), 50, 30, 45],
[date(2015,9, 4), 30, 25, 40],
[date(2015,9, 5), 25, 35, 30],
[date(2015,9, 6), 20, 40, 35],
]

for row in rows:
  ws.append(row)

c1 = LineChart()
c1.title = "Line Chart"
c1.style = 13
c1.y_axis.title = 'Size'
c1.x_axis.title = 'Test Number'

data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
c1.add_data(data, titles_from_data=True)

# Style the lines
s1 = c1.series[0]
s1.marker.symbol = "triangle"
s1.marker.graphicalProperties.solidFill = "FF0000" # Marker filling
s1.marker.graphicalProperties.line.solidFill = "FF0000" # Marker outline

s1.graphicalProperties.line.noFill = True

s2 = c1.series[1]
s2.graphicalProperties.line.solidFill = "00AAAA"
s2.graphicalProperties.line.dashStyle = "sysDot"
s2.graphicalProperties.line.width = 100050 # width in EMUs

s2 = c1.series[2]
s2.smooth = True # Make the line smooth
ws.add_chart(c1, "A10")
wb.save("line.xlsx")
Reply
#3
IT appears you have to integrate this into your code
from openpyxl.chart.text import RichText
from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties, Font    

font_test = Font(typeface='Calibri')
cp = CharacterProperties(latin=font_test, sz=1500)
chart.x_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)])
Recommended Tutorials:
Reply
#4
To... metulburr
I tried it before this posting
but
Font size of Chart Title and x_axis.Title can not be changed....
If you can .... can you give me some examples ?
Reply
#5
im not familiar with that library. That is just what i found googling.

You can make a request to the authors of that library through issues. I dont see that in their tutorials, so it might be worth them taking a look at and if not they could much more easily lead you to the answer.
https://bitbucket.org/openpyxl/openpyxl/issues
Recommended Tutorials:
Reply
#6
There are two methods to style chart text elements. Axis numbers can be styled using chart.text RichText properties and chart titles using drawing.text ParagraphProperties. The legend can be styled using either method.

from openpyxl.chart import ScatterChart, Reference, Series
from openpyxl.chart.text import RichText
from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties, RichTextProperties, Font, RegularTextRun

# Set up basic XY Scatter Chart
chart = ScatterChart()
chart.title = "Scatter Chart"
chart.style = 2 # Excel menu chart tools: Design > Chart Styles
chart.x_axis.title = 'X-axis Title'
chart.y_axis.title = 'Primary Y-axis Title'

# Style chart
# X and Y axes numbers
font = Font(typeface='Verdana')
size = 1400 # 14 point size
cp = CharacterProperties(latin=font, sz=size, b=False) # Not bold
pp = ParagraphProperties(defRPr=cp)
rtp = RichText(p=[Paragraph(pPr=pp, endParaRPr=cp)])
chart.x_axis.txPr = rtp        # Works!
chart.y_axis.txPr = rtp        # Works!

# X and Y axes titles
chart.x_axis.title.tx.rich.p[0].pPr = pp       # Works!
chart.y_axis.title.tx.rich.p[0].pPr = pp       # Works!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for alignment and font size 1418 0 302 Jan-14-2024, 03:56 AM
Last Post: 1418
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 2,067 Sep-14-2023, 06:54 AM
Last Post: Mehboob
  read active document name - other than from the window title ineuw 0 522 Sep-11-2023, 09:06 AM
Last Post: ineuw
  Change font in a list or tuple apffal 4 2,666 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  Problem with scraping the Title from a web page Wagner822 0 675 Jun-29-2022, 11:31 PM
Last Post: Wagner822
  Change Text Size in Console? ShakeyPakey 9 11,155 Dec-09-2021, 02:51 AM
Last Post: drvlwho
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,300 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  how to change the font style on Linux? laylalogan337 2 1,930 Aug-10-2021, 04:31 PM
Last Post: deanhystad
  [split] Change Text Size in Console? CodingKid 1 1,865 Jul-20-2021, 02:47 PM
Last Post: deanhystad
  Application to find title in PDF? Winfried 4 3,595 Mar-25-2021, 03:00 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020