2

I am comparing sales data from 9 shops:

enter image description here

However the title is very small, and seems to be inside the chart. I can't figure out how to change this behaviour or font size.

Using plt.title does not work - it only edits the final subplot title.

I also tried to add title() call at the end of the pandas .plot() method, but this causes an error: 'numpy.ndarray' object has no attribute 'title'

How can I edit the title font size and position at the top?

My code:

# plot the data
df.set_index('local_date').plot(kind='line', subplots=True, grid=True,figsize=(30,20),lw=3.5,linestyle='--', title="Sample Data (Unit)",
    layout=(4, 3), sharex=True, sharey=True, legend=False)

# edit the legends
[ax.legend(loc=2,prop={'size': 20}) for ax in plt.gcf().axes]

#plt.title(title,fontsize=20) # this only edits final plot title
plt.tight_layout()

And sample data for pd.DataFrame.fromt_dict():

from pandas import Timestamp

myd = {'local_date': {0: Timestamp('2020-01-06 00:00:00'),
  1: Timestamp('2020-01-13 00:00:00'),
  2: Timestamp('2020-01-20 00:00:00'),
  3: Timestamp('2020-01-27 00:00:00'),
  4: Timestamp('2020-02-03 00:00:00'),
  5: Timestamp('2020-02-10 00:00:00'),
  6: Timestamp('2020-02-17 00:00:00'),
  7: Timestamp('2020-02-24 00:00:00'),
  8: Timestamp('2020-03-02 00:00:00'),
  9: Timestamp('2020-03-09 00:00:00'),
  10: Timestamp('2020-03-16 00:00:00'),
  11: Timestamp('2020-03-23 00:00:00'),
  12: Timestamp('2020-03-30 00:00:00')},
 'shop1': {0: 100.0,
  1: 156.0,
  2: 1231.0,
  3: 360.0,
  4: 250.0,
  5: 150.0,
  6: 1287.0,
  7: 397.0,
  8: 546.0,
  9: 270.0,
  10: 50.0,
  11: 1464.0,
  12: 280.0},
 'shop2': {0: 0.0,
  1: 430.0,
  2: 270.0,
  3: 900.0,
  4: 665.0,
  5: 750.0,
  6: 780.0,
  7: 360.0,
  8: 704.0,
  9: 480.0,
  10: 470.0,
  11: 0.0,
  12: 730.0},
 'shop3': {0: 222.0,
  1: 258.0,
  2: 255.0,
  3: 367.0,
  4: 332.0,
  5: 324.0,
  6: 269.0,
  7: 336.0,
  8: 492.0,
  9: 349.0,
  10: 329.0,
  11: 354.0,
  12: 581.0},
 'shop4': {0: 607.0,
  1: 140.0,
  2: 172.0,
  3: 89.0,
  4: 63.0,
  5: 700.0,
  6: 157.0,
  7: 401.0,
  8: 112.0,
  9: 180.0,
  10: 76.0,
  11: 104.0,
  12: 346.0},
 'shop5': {0: 51.0,
  1: 375.0,
  2: 78.0,
  3: 92.0,
  4: 59.0,
  5: 193.0,
  6: 137.0,
  7: 96.0,
  8: 331.0,
  9: 195.0,
  10: 291.0,
  11: 347.0,
  12: 190.0},
 'shop6': {0: 150.0,
  1: 225.0,
  2: 345.0,
  3: 150.0,
  4: 230.0,
  5: 0.0,
  6: 500.0,
  7: 150.0,
  8: 0.0,
  9: 230.0,
  10: 50.0,
  11: 270.0,
  12: 0.0},
 'shop7': {0: 39.0,
  1: 130.0,
  2: 278.0,
  3: 0.0,
  4: 0.0,
  5: 585.0,
  6: 175.0,
  7: 0.0,
  8: 360.0,
  9: 79.0,
  10: 135.0,
  11: 0.0,
  12: 0.0},
 'shop8': {0: 74.0,
  1: 66.0,
  2: 75.0,
  3: 95.0,
  4: 158.0,
  5: 50.0,
  6: 233.0,
  7: 117.0,
  8: 146.0,
  9: 142.0,
  10: 261.0,
  11: 30.0,
  12: 85.0},
 'shop9': {0: 74.0,
  1: 146.0,
  2: 166.0,
  3: 6.0,
  4: 75.0,
  5: 22.0,
  6: 33.0,
  7: 18.0,
  8: 15.0,
  9: 110.0,
  10: 150.0,
  11: 144.0,
  12: 561.0}}
SCool
  • 3,104
  • 4
  • 21
  • 49
  • 1
    Can you try: `plt.gcf().suptitle(...)`? – mechanical_meat May 01 '20 at 12:14
  • This kind of works - I can now edit the font size, thanks. But the title is stuck inside the second subplot ("shop2") – SCool May 01 '20 at 12:19
  • I just loaded your data and saw that. Can you get away with removing the line: `plt.tight_layout()`? The title is ok with that removed. – mechanical_meat May 01 '20 at 12:20
  • Yes it looks like the `tight_layout` messes with the title. After some more googling I found that calling `plt.subplots_adjust(top=0.93)` after calling `tight_layout` fixes it. From here: https://stackoverflow.com/questions/8248467/matplotlib-tight-layout-doesnt-take-into-account-figure-suptitle – SCool May 01 '20 at 12:26
  • You can also change the position of the title with the `y=` argument. `plt.gcf().suptitle("Title", y=0.95)`, where y is the y location of the text in figure coordinates – DavidG May 01 '20 at 13:57

1 Answers1

2

The overall chart label is controlled with plt.gcf().suptitle().

Then, in order to put the title outside the plots you can do this (from here: https://stackoverflow.com/a/45161551/42346):

plt.gcf().tight_layout(rect=[0, 0.03, 1, 0.95])

So the full example would be:

df.set_index('local_date').plot(kind='line', subplots=True, grid=True,figsize=(30,20),
             lw=3.5,linestyle='--', layout=(4, 3), sharex=True, sharey=True, legend=False) 

# edit the legends 
[ax.legend(loc=2,prop={'size': 20}) for ax in plt.gcf().axes] 

plt.gcf().tight_layout(rect=[0, 0.03, 1, 0.95])
plt.gcf().suptitle("Sample Data (Unit)",fontsize=20)
plt.show()

Result:
enter image description here

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223