Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Python Visual Automatic Scaling and Aspect Ratio Based on Visual Dimensions on Report Canvas

Hi,

 

is there a way to automatically scale python plots based on the dimensions of the visual in a repart? I know, I can scale python plots inside the visual manually...

 

#Setting up the base plot size
matplotlib.pyplot.tight_layout()
fig, ax = plt.subplots(figsize = (5,1.5))
 
... but I would preferably get rid of the magic numbers in the above code so that I can easily resuse plotting code across different visuals and get a consistent appearance without having to play with the scaling manually to get the dimensions right.
 
In other words: are there any variables passed to the python code that allow me to get information about the area in which the generated PNG is "placed" inside the python visual?
 
Cheers, AAB
1 ACCEPTED SOLUTION
v-henryk-mstf
Community Support
Community Support

Hi @Anonymous ,

 

Based on your description, this does not seem to be possible in powerbi. Or you can provide a relevant screenshot and describe it, and I will try to give you as much helpful information as possible.


Looking forward to your reply.


Best Regards,
Henry

 

View solution in original post

2 REPLIES 2
v-henryk-mstf
Community Support
Community Support

Hi @Anonymous ,

 

Based on your description, this does not seem to be possible in powerbi. Or you can provide a relevant screenshot and describe it, and I will try to give you as much helpful information as possible.


Looking forward to your reply.


Best Regards,
Henry

 

Anonymous
Not applicable

Well, noone else has an idea how to pull this off, so I accept that solution 🙂

 

It would have been great if PowerBI provided some "hidden arguments" to the wrapping python function that executes the user code with basic information such as the above.

 

Also, I just ran the following code...

 

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

# dataset = pandas.DataFrame(DataInfo)
# dataset = dataset.drop_duplicates()

# Paste or type your script code here:

import os
f = open('foo.txt','w')
sys.stdout = f
f.write('dir: ------------\n')
print(dir())
f.write('globals:---------\n')
print(globals())
f.write('locals: ---------\n')
print(locals())
f.write('-----------------')
f.close()
os.system("notepad.exe foo.txt")

 

... and I cannot see anything in the scope of the wrapper function that hints upon this functionality. 

 

I did not really expect it to do this but I am gald I asked; it is an MS product after all 😉

 

Cheers, Andy

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors