I am trying to create a sunburst plot using Plotly. Everything is working fine except that after exporting that newly created SVG file into Overleaf and then creating PDF using LaTeX code, the image looks so weird. The texts are getting out of the image and overlapping with each other. Check the demo image here.
Here is the code I used to produce the image.
import pandas as pd
import plotly.express as px
import os
df = pd.read_excel('data/countries.xlsx')
df.head()
fig = px.sunburst(df, path=['Continent', 'Country'])
fig.show()
graphics_dir = "graphics"
if not os.path.exists(graphics_dir):
os.mkdir(graphics_dir)
fig.write_image(format='svg', file='{}/countries.svg'.format(graphics_dir))
I know how to fix this problem when I am generating graphics using Matplotlib from this link and it is working for me. But I do not know how to achieve this in Plotly.
I have managed to hack the whole process with advice from one of my colleagues. As I could not disable the font path from SVG, I have chosen another method.
First, I converted the SVG file into PDF, and then I used the PDF instead of that SVG file. The benefit of converting SVG to PDF is that the font path of the SVG is no more available in the PDF. They become embedded into the PDF.
This way, I have managed to fix the issue! Thanks.
Related
unlike google colab i cant right click to save the image, i use vscode on a chromebook with linux beta, it would be helpful if you provide step-by-step instruction. I am also new to code and the notebook uses python
p.s this is for dall.e flow-jupyter
On the right we have two options, first one is to expand image (or zoom) and second option is to save image.
But best option is to use code to save image instead of saving images manually.
For example, if you want save pandas graph, then
import pandas as pd
import matplotlib.pyplot as plt # pandas uses matplotlib in the backend
df = pd.DataFrame({"a": list(range(50))})
df["a"].plot()
plt.savefig("test.png")
Is there any way to plot 2D array as an image using Bokeh with interpolation like in Matplotlib? I am able to plot using an example: https://docs.bokeh.org/en/latest/docs/gallery/image.html
However, the image is to coarse. I like the way interpolation work in Matplotlib: https://matplotlib.org/gallery/images_contours_and_fields/interpolation_methods.html
I tried to perform interpolation beforehand but the matrix size now is to big.
I had the same issue and I've found the answer in pyviz's Gitter.
The solution combines Holoviews and Datashader:
import holoviews as hv
from holoviews import opts
from holoviews.operation.datashader import regrid
img = hv.Image(data)
regrid(img, upsample=True, interpolation='bilinear')
If you are working with a large dataset then you could try Bokeh in combination with Datashader/HoloViews like in this example. When zooming in, Datashader can dynamically create new high quality images from your data that could be displayed in your Bokeh plot.
Not an answer but an observation - I've noticed that plotting an image via an image_url source it appears interpolated when zoomed in whilst if you read in the same image and display it from a columndatasource via 'image' it then appears blocky when zoomed. I'd love to know how to make it appear interpolated too when zoomed, eg like the raw png image appears. Holoview/datashader would be a great solution but in my case I need it to work offline/as a standalone html file.
I've just created a dashboard in Dash, which fits automatically to my computer monitor and looks half decent. I now have the problem that I have to produce a way to print to PDF.
I've seen the Vanguard example which uses external CSS to format into an A4-size, and then a print button (using external javascript?). Is there a more 'Dash-native' way of doing this that doesn't require me to learn another programming language (I only know Python)?
There doesn't seem to be anything on the Dash User Guide https://dash.plot.ly/, all I've found is this https://plot.ly/python/pdf-reports/ which describes how to print individual plotly figures to pdf.
Ideally, it would format as it is now online (online layout) without losing space down the sides, and then I could use another layout (pdf layout) which could be printed out.
Any links or suggestions on how to proceed with this would be very much appreciated!
You can get pdf from plotly if save plot as png image (the most left option on image):
And then using that code convert png to pdf:
from PIL import Image
# Save your plot as png file (option "Download as png" in upper-left corner)
PNG_FILE = 'horizontal-bar.png'
PDF_FILE = 'horizontal-bar.pdf'
rgba = Image.open(PNG_FILE)
rgb = Image.new('RGB', rgba.size, (255, 255, 255)) # white background
rgb.paste(rgba, mask=rgba.split()[3]) # paste using alpha channel as mask
rgb.save(PDF_FILE, 'PDF', resolution=100.0)
If you are a lazy one, you can use library webkit2png check here how. This allowing you not clicking on Download as png button and just convert from html to png and then to pdf.
In various cases whenever I use latex in matplotlib, I am getting a very pixely appearance when rendering the figure to an image. When I view the figure in interactive mode it looks fine. For example, I'm setting the yaxis label with:
'Emissions Flux '+r'($\mathregular{(\mu g/m^2 s)}$'
I'm also setting the twin y axis to a log scale and the eponents are presumably latex as well. Non latex text is crisp.
I as far I understand the problem your images are too pixelated. Often this is the result of saving an image using a bitmap format. To receive better images one should export them to vector-graphs, like for example pdf.
To export images as vector-graphs your save statement should be something like:
myfig.savefig('myfig.pdf', format='pdf')
A clear explanation about bitmaps and vector graphs: http://www.prepressure.com,
an important source of information concerning matlibplot: http://matplotlib.org
I would like to import two png files and stich them into subplots using matplotlib. I am following this tutorial to do this. But when I save the figure with a 2x2 subplot, the resolution is very poor. Is there a better way of doing this?
If the resolution is satisfactory before you save, try using the dpi keyword along with matplotlib.pyplot.savefig() (see docs page for matplotlib.pyplot.savefig). Once you have the plot generated, simply type
from matplotlib.pyplot import savefig
savefig( 'stitched.png', dpi=400 )
and hopefully this results in a satisfactory png.