matplotlib / mplfinance

Financial Markets Data Visualization using Matplotlib
https://pypi.org/project/mplfinance/
Other
3.64k stars 628 forks source link

X-Axis Date and Time series #573

Closed tdavidge closed 1 year ago

tdavidge commented 1 year ago

I am trying to create a 5 day chart of Intraday Stock data using mplfinance. From Open to Close only, no data gaps. I can get a chart to save and/or show but am struggling with formatting the grid spacing and the x-axis interval/labels. I have been trying to get this working in matplotlib for what seems like an age now and I just cannot get it work. At least with mplfinance I was able to get a chart to work. I'm simply not good enough at python coding to understand how to format the xaxis I am afraid.

My question obviously is how to format grid and x-axis to line the grid with at least the open of the day (as in on change of date) so I can see the overnight GAP, and say a frequency of hourly between 09:30am and 4:00pm. I apologize in advance for my ignorance but this is all kinda new to me and having been trying all manner of examples that I do not understand, this is my last resort.

Any assistance would be very much appreciated. Using python 3.7 and mplfinance v0.12.9b5 on windows 11 pro.

import sys, os, datetime, time, warnings #csv
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mplfinance as mpf
from datetime import datetime

sym = sys.argv[1] #symbol name
act_dt = sys.argv[2] #active date of the required process no quotes
act_int = sys.argv[2].replace('/', '-')#this converts the incoming dates into values that can be used in the savefig filename

#the following is the dataframe df
Date    Open    High     Low   Close

2022-11-17 09:30:00  332.73  333.60  332.73  333.54
2022-11-17 09:31:00  333.52  333.70  333.39  333.55
2022-11-17 09:32:00  333.55  333.88  333.47  333.77
2022-11-17 09:33:00  333.76  333.92  333.76  333.86
2022-11-17 09:34:00  333.67  333.67  333.67  333.67
                ...     ...     ...     ...     ...
2022-11-25 13:36:00  343.60  343.60  343.45  343.45
2022-11-25 13:41:00  343.50  343.50  343.50  343.50
2022-11-25 13:46:00  343.47  343.47  343.47  343.47
2022-11-25 13:48:00  343.20  343.20  343.20  343.20
2022-11-25 13:49:00  343.47  343.47  343.47  343.47

df.columns = ['Date', 'Open', 'High', 'Low', 'Close']

df.set_index('Date')
df.index = pd.to_datetime(df.Date)
print(df)

mc = mpf.make_marketcolors(up='g',down='r',inherit=True)
s  = mpf.make_mpf_style(marketcolors=mc, gridaxis='both', gridstyle='dotted')

mpf.plot(df,style=s, 
    datetime_format='%b %d',
    type='line',
    title='\n'+sym+' '+act_int, 
    ylabel='Price',
    xrotation=90,
    tight_layout=True,
    figratio=(12,5),
    figscale=1,
    returnfig=True,
    savefig=dict(fname='ohlc_wide_'+sym+'_'+act_int+'.jpg',dpi=125, quality=95, bbox_inches='tight'),
    )
mpf.show()

This gives me the attached chart.

ohlc_wide_DIA_11-25-2022

tdavidge commented 1 year ago

So, I am making progress albeit with great difficulty. With the help of this I have managed to get the first attachement by show and then manually saving. However, when using savefig I get the second attachement. chart1 chart2

My questions now are a) how to get the short date (no year) to appear horizontally centered below each of the 5 days time bars b) why the difference between showing and saving the output ? c) why is the show result offset to the right ? in the display window I cannot see the full price, not is there a ylabel

code:

import sys, os, datetime, time, warnings #csv
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mplfinance as mpf
from datetime import datetime

import pyodbc #database connectivity
cnxn = pyodbc.connect(database connection)

sym = sys.argv[1] #symbol name
act_dt = sys.argv[2] #active date of the required process no quotes
act_int = sys.argv[2].replace('/', '-')#this converts the incoming dates into values that can be used in the savefig filename

df=pd.read_sql_query(" \
database query
",cnxn, )

df.columns = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']

df.set_index('Date')
df.index = pd.to_datetime(df.Date)
print(df)

start_date = df['Date'].iloc[0]
end_date = df['Date'].iloc[-1]

print(start_date)
print(end_date)

fig, axlist= mpf.plot(df,
    style='yahoo', 
    type='line', 
    title='\n'+sym+' '+act_int, 
    volume=False, 
    xrotation=90, 
    ylabel='Price',
    figsize=(15,7), 
    figscale=1,
    returnfig=True,
    tight_layout=True,
    show_nontrading=False,
    #savefig=dict(fname='ohlc_wide_'+sym+'_'+act_int+'.jpg',dpi=125, quality=95, bbox_inches='tight')
    )

#idx = pd.date_range(start_date, end_date, freq='30T')
idx = pd.date_range(start_date, end_date, freq='1H')
df_label_idx = pd.DataFrame(index=idx)

df_label = pd.merge(df_label_idx, df, how='inner', left_index=True, right_index=True )

# Tick labels are generated based on df_label
tick_labels = list(df_label.index.strftime('%H:%M'))
ticklocations = [df.index.get_loc(tick) for tick in df_label.index ]

axlist[-2].xaxis.set_ticks(ticklocations)
axlist[-2].set_xticklabels(tick_labels)

mpf.show()
DanielGoldfarb commented 1 year ago

This is not a trivial problem, but it is do-able, just a bit tricky. Also, for what it's worth, I am hoping at some point to build a more generalized solution into mplfinance to make it easier for users to do things like this. In the meantime this is what we have and you are mostly on the right track.

First, I have some questions about your questions because I don't understand them entirely.

"a) how to get the short date (no year) to appear horizontally centered below each of the 5 days time bars" Is your question about formatting (not showing a year) or about where the tick appears (in the middle of each day vs the beginning of each day)? I am fairly certain that the tick label must appear where the tick itself is. But there may be a matplotlib work-around (that I have not tried yet) such as this. If that is what this question is about (shifting the tick labels left or right relative to the ticks themselves) it can be done but lets first focus on getting the exact ticks that you want.

"b) why the difference between showing and saving the output ?" What exactly is the difference between the First Image, and this Second Image? It sounds like you are implying that the code is exactly the same except for how you saved the plot, which at first doesn't make sense to me because the x-axis is clearly formatted differently. Maybe I'm missing something. On the other hand, indeed it could be ... I faintly recall (will have to try to find the issue/discussion) seeing something, maybe a year ago, about matplotlib's savefig method doing it's own axis formatting, ignoring what you otherwise specified when calling .show(). I will see if I can find that.

"c) why is the show result offset to the right ? in the display window I cannot see the full price, not is there a ylabel" It could be due to tight_layout. Try it without tight_layout. If that is the issue, you may be able to use tight_layout in combination with scale_padding. See this answer for more information about scale_padding. You may need to increase (scale up) the padding on the right to provide additional space for the y-axis label.


That said, ultimately what do you want? Would you be satisfied with one date label per day at the beginning of each day, or maybe at the middle of each day? I'm going to play around and see what I can get to work.

DanielGoldfarb commented 1 year ago

I worked up some sample code. Let me know if this works for you:

import mplfinance as mpf
import yfinance as yf
import datetime

df = yf.download('DIA',start='2022-11-07',end='2022-11-15',interval='15m')

ticks = []
tlabs = []
dates = sorted(list(set([d.date() for d in df.index])))
for d1 in dates:
    d2 = d1 + datetime.timedelta(days=1)
    ts = df.loc[d1:d2].index[0]  # first timestamp of each day
    ticks.append(df.index.get_loc(ts))
    tlabs.append(d1.strftime('%b %d'))

fig, axlist = mpf.plot(df,type='candle',xrotation=0,style='yahoo',tight_layout=True,returnfig=True)
axlist[-2].set_xticks(ticks,labels=tlabs,ha='left')

mpf.show()

image

tdavidge commented 1 year ago

Hello Daniel and thanks for the response, a great relief to get some help with this. I copied your exact code, but get the following error when executed: C:\TEMP\PYTHON>python test_multiple_days.py [*100%***] 1 of 1 completed Traceback (most recent call last): File "test_multiple_days.py", line 17, in axlist[-2].set_xticks(ticks,labels=tlabs,ha='left') TypeError: set_xticks() got an unexpected keyword argument 'labels'


From: Daniel Goldfarb @.> Sent: Friday, December 2, 2022 2:29 PM To: matplotlib/mplfinance @.> Cc: tdavidge @.>; Author @.> Subject: Re: [matplotlib/mplfinance] X-Axis Date and Time series (Issue #573)

I worked up some sample code. Let me know if this works for you:

import mplfinance as mpf import yfinance as yf import datetime

df = yf.download('DIA',start='2022-11-07',end='2022-11-15',interval='15m')

ticks = [] tlabs = [] dates = sorted(list(set([d.date() for d in df.index]))) for d1 in dates: d2 = d1 + datetime.timedelta(days=1) ts = df.loc[d1:d2].index[0] # first timestamp of each day ticks.append(df.index.get_loc(ts)) tlabs.append(d1.strftime('%b %d'))

fig, axlist = mpf.plot(df,type='candle',xrotation=0,style='yahoo',tight_layout=True,returnfig=True) axlist[-2].set_xticks(ticks,labels=tlabs,ha='left')

mpf.show()

[image]https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F11164790%2F205370405-1439ae45-0ce1-4eea-8925-d56904ec36ef.png&data=05%7C01%7C%7C9859036347e54cb1c81908dad49b8b66%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638056061764653292%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=jSVLLPNSIyvW%2FBjiHjrPMtZjCa7F21RToKm6%2F8btass%3D&reserved=0

— Reply to this email directly, view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmplfinance%2Fissues%2F573%23issuecomment-1335718597&data=05%7C01%7C%7C9859036347e54cb1c81908dad49b8b66%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638056061764653292%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=D4RA0qwN3eoFeg43Bw6qfE40Ob5NQb2E6BWZ6WC%2F2ns%3D&reserved=0, or unsubscribehttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FA4OQSNZYIIR2URMXHETKUTDWLJEZ5ANCNFSM6AAAAAASNRL6PM&data=05%7C01%7C%7C9859036347e54cb1c81908dad49b8b66%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638056061764653292%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=NdukdCd%2FRwUBawalP7Y7F3fk6blboORbni5qVpSrgKI%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.***>

DanielGoldfarb commented 1 year ago

Perhaps you have an older version of matplotlib. Don't upgrade just yet as that may introduce too many new changes. Rather try splitting up setting ticks and labels as in your code:

axlist[-2].xaxis.set_ticks(ticks)
axlist[-2].set_xticklabels(tlabs)

(

tdavidge commented 1 year ago

Site packages folder name = mplfinance-0.12.9b5.dist-info

Ok that worked and displays fine with mpf.show(), however adding savefig tries to add a horizonal time and does this: @. Mpl.show() does this, see what I mean about the offset ? @.

From: Daniel @.> Sent: Friday, December 2, 2022 4:15 PM To: @.> Cc: @.>; @.> Subject: Re: [matplotlib/mplfinance] X-Axis Date and Time series (Issue #573)

Perhaps you have an older version of matplotlib. Don't upgrade just yet as that may introduce too many new changes. Rather try splitting up setting ticks and labels as in your code:

axlist[-2].xaxis.set_ticks(ticks)

axlist[-2].set_xticklabels(tlabs)

(

— Reply to this email directly, view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmplfinance%2Fissues%2F573%23issuecomment-1335851309&data=05%7C01%7C%7C8e12e097192d4f2bd96708dad4aa536d%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638056125244319208%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=LxgSQgvQ%2BCkyvnTYqESoVZl2OOB7yT0APHKOFJnFjTk%3D&reserved=0, or unsubscribehttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FA4OQSN3SKVJENOLILMXHWRLWLJRGXANCNFSM6AAAAAASNRL6PM&data=05%7C01%7C%7C8e12e097192d4f2bd96708dad4aa536d%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638056125244319208%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=E%2BQ0v4rVeNbWKfpR4OemkT9ph5JVlKvPsMEFK56r%2FN0%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.***>

tdavidge commented 1 year ago

Hey Daniel, to try to clarify my somewhat cryptic issues .. heh a) I'd like to be able to retain the x-axis time ticks, but add the date as a say 2nd sub-label below them. So your chart would be great, if it also included vertical hourly time ticks in the grid. I remember seeing this done somewhere (I think it was with plotly). In the end I need to be able to save same to a full page A4 landscaped jpg, which will be printed and also included in a xlsx spreadsheet. I've just spoken with my partner and at this point, it is more important to get the date right and leave the time ticks for another effort.

b) There is definately something not right with the savefig, as you can tell from the two images I posted. They were created with the exactly the same code, with and without a savefig attribute in the mpf.plot command. I tried with just savefig(filename) and it still came out different.

Ultimately, your chart would be great, date to the beginning of the day is just fine thanks, albeit, show and savefig need to be the same result .. heh.


From: Daniel Goldfarb @.> Sent: Friday, December 2, 2022 1:41 PM To: matplotlib/mplfinance @.> Cc: tdavidge @.>; Author @.> Subject: Re: [matplotlib/mplfinance] X-Axis Date and Time series (Issue #573)

This is not a trivial problem, but it is do-able, just a bit tricky. Also, for what it's worth, I am hoping at some point to build a more generalized solution into mplfinance to make it easier for users to do things like this. In the meantime this is what we have and you are mostly on the right track.

First, I have some questions about your questions because I don't understand them entirely.

"a) how to get the short date (no year) to appear horizontally centered below each of the 5 days time bars" Is your question about formatting (not showing a year) or about where the tick appears (in the middle of each day vs the beginning of each day)? I am fairly certain that the tick label must appear where the tick itself is. But there may be a matplotlib work-around (that I have not tried yet) such as thishttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F28615887%2Fhow-to-move-a-tick-label-in-matplotlib&data=05%7C01%7C%7C64e804c8239441abe1fd08dad494c2ec%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638056032632281954%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=A9AYWPDgPQ3u1Tclq3YEIlteBWKDg7Fu3K6S2BskdnE%3D&reserved=0. If that is what this question is about (shifting the tick labels left or right relative to the ticks themselves) it can be done but lets first focus on getting the exact ticks that you want.

"b) why the difference between showing and saving the output ?" What exactly is the difference between the First Imagehttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F119343415%2F204861777-c07bea76-f492-44a7-aabe-f6fdff19c9f4.png&data=05%7C01%7C%7C64e804c8239441abe1fd08dad494c2ec%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638056032632281954%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=TFTjlGNsWs3p4wT%2FvB5yDjHPgyKPmqNjGQl19QfEHjM%3D&reserved=0, and this Second Imagehttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F119343415%2F204861780-16b3aacd-dc5a-44ae-8bf5-48f936898d1e.jpg&data=05%7C01%7C%7C64e804c8239441abe1fd08dad494c2ec%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638056032632281954%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=CQTGs783QCIIeZNww59K%2BGqfqnrCxM1cTyghsegCqPE%3D&reserved=0? It sounds like you are implying that the code is exactly the same except for how you saved the plot, which at first doesn't make sense to me because the x-axis is clearly formatted differently. Maybe I'm missing something. On the other hand, indeed it could be ... I faintly recall (will have to try to find the issue/discussion) seeing something, maybe a year ago, about matplotlib's savefig method doing it's own axis formatting, ignoring what you otherwise specified when calling .show(). I will see if I can find that.

"c) why is the show result offset to the right ? in the display window I cannot see the full price, not is there a ylabel" It could be due to tight_layout. Try it without tight_layout. If that is the issue, you may be able to use tight_layout in combination with scale_padding. See this answer for more information about scale_paddinghttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmplfinance%2Fissues%2F212%23issuecomment-655603896&data=05%7C01%7C%7C64e804c8239441abe1fd08dad494c2ec%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638056032632281954%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=rRcM31ybThUgisosdgh5y20FbI6hOjnYbJBh0difXiI%3D&reserved=0

That said, ultimately what do you want? Would you be satisfied with one date label per day at the beginning of each day, or maybe at the middle of each day? I'm going to play around and see what I can get to work.

— Reply to this email directly, view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmplfinance%2Fissues%2F573%23issuecomment-1335668401&data=05%7C01%7C%7C64e804c8239441abe1fd08dad494c2ec%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638056032632281954%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=xi%2BbOEcdYYElaqcsTnQIrArOpQfjBzXOiFi1wmY8Sjc%3D&reserved=0, or unsubscribehttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FA4OQSN6NIULE7BNLBMAZPK3WLI7D3ANCNFSM6AAAAAASNRL6PM&data=05%7C01%7C%7C64e804c8239441abe1fd08dad494c2ec%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638056032632281954%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=ZK4q3z96Vczn%2FYooonPBbakNG6JUk4ZGACR%2FxANT4VQ%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.***>

DanielGoldfarb commented 1 year ago

a) I saw once that there is a way (in matplotlib) to create "two level" axis labels, so that you have one level showing the times (perhaps every two hours) and another level showing the date, but I would have to research how to do it. A simpler solution, that may not be exactly what you want, would be to format the datetime to include both date and time and modify the code to show maybe 2 or 3 ticks per day (instead of 1 tick per day as I had it). Alternatively, do major ticks as I showed (one per day with date), and do minor ticks perhaps hourly.

b) Yes, as I mentioned before there is definitely some kind of a problem with savefig() tries to do its own axis formatting. I ran into the once before and can't remember what the solution was. I am going to spend a little time looking into this savefig issue,

DanielGoldfarb commented 1 year ago

I did some playing around, and I was able to get both dates and times on separate levels of the tick labels, using major and minor ticks. Please let me know if this is something like what you are trying to do:

ticks = []
tlabs = []
mitks = []
milab = []
dates = sorted(list(set([d.date() for d in df.index])))

for d1 in dates:

    # Major Ticks:
    d2 = d1 + datetime.timedelta(days=1)
    ts = df.loc[d1:d2].index[0]
    ticks.append(df.index.get_loc(ts))
    # line feed at beginning of label so it appears a lower below the x-axis:
    tlabs.append('\n'+d1.strftime('%b %d'))

    # Minor Ticks:
    mitks.append(ticks[-1]+0.1)
    milab.append('09:30')
    ts = df.loc[str(d1)+' 13:00':d2].index[0]
    mitks.append(df.index.get_loc(ts))
    milab.append('13:00')

fig, axlist = mpf.plot(df,type='candle',xrotation=0,style='yahoo',tight_layout=True,returnfig=True,figratio=(2,1))

axlist[-2].set_xticks(ticks)
axlist[-2].set_xticklabels(tlabs, ha='left')
axlist[-2].set_xticks(mitks,minor=True)
axlist[-2].set_xticklabels(milab, ha='center', minor=True, rotation=0)

mpf.show()

The result:

image

tdavidge commented 1 year ago

Hey Daniel,

Thank you so much for your efforts, that is excellent and if we can resolve the savefig issues is exactly what I was after. I am however still getting that weird right hand offset when showing the figure and am losing the y axis and labels as a result. The savefig behavior has also changed and now shows the attached. Note savefig displays the y axis correctly, but messes with the timing ticks.

DanielGoldfarb commented 1 year ago

@tdavidge First, please be aware that there is a bug in GitHub when replying via email.
To post or format something to this discussion, please navigate directly to this issue page on the website and post here.
(Even though the email says that you can reply, it does not work correctly; for example, see here).

Regarding savefig, please try the following. It worked for me.

Do not use the savefig kwarg of mpf.plot(). Rather, call savefig() directly on the Figure object as in the following code, and use the bbox_inches='tight' kwarg of savefig:

fig, axlist = mpf.plot(df,type='candle',xrotation=0,style='yahoo',tight_layout=True,returnfig=True,figratio=(2,1))

axlist[-2].set_xticks(ticks)
axlist[-2].set_xticklabels(tlabs, ha='left')
axlist[-2].set_xticks(mitks,minor=True)
axlist[-2].set_xticklabels(milab, ha='center', minor=True, rotation=0)

fig.savefig( 'myplot.pdf', bbox_inches='tight' )

That should work OK. If not, please post the images from the two ways of saving (using the above code). But again, do not post via email since that doesn't work. Please post directly to the GitHub website. Thanks.

tdavidge commented 1 year ago

Hi Daniel, that worked great. Duly noted on the email reply, my apologies. Thanks again for all the assistance, I am now trying to port your code to some existing charts that I was struggling with that use a different dataframe format to be able to plot multiple series across a similar timeframe. Will let you know how I get on.

DanielGoldfarb commented 1 year ago

... that worked great

Glad to hear it.

I am now trying to port your code to some existing charts ...

Good luck. If you are willing, please share the charts once you get them looking the way you want. It's always exciting to see the things people are doing with mplfinance.

All the best. --Daniel