1 year ago
#72323
MikkelC
Plotly express boxplot image export - part of box colors missing in svg file
I have made a boxplot using plotly express (px.box), and the resulting plot looks good in my Google Chrome / Jupyter browser window. Here is a schreenshot of two randomly selected boxes and they look as I expect.
However, after exporting using pio.write_image, it looks like this (zoomed in):
WHY does it not fill up the whole box after export? What can I do to avoid it? I have tried defining width and height as "size*300" to set the DPI to 300, I have tried with and without "scale" and I have tried to use Orca as image export engine, tried export as .PDF, and updated Plotly (Plotly vers. 5.1.0). Links to comprehensive guides for export of high-quality plotly plots for use as figures in scientific papers also much appreciated, as exporting quality very often not satifying.
A example of the problem can be reproduced with this:
import plotly.express as px
import plotly.io as pio
import pandas as pd import plotly.graph_objs as go import sys import plotly
pio.templates.default = “simple_white”
x = [‘Cat1’, ‘Cat1’,‘Cat2’, ‘Cat2’, ‘Cat3’, ‘Cat3’, ‘Cat4’, ‘Cat4’,‘Cat5’, ‘Cat5’, ‘Cat6’, ‘Cat6’, ‘Cat6’, ‘Cat7’, ‘Cat7’, ‘Cat8’, ‘Cat8’,‘Cat11’, ‘Cat11’, ‘Cat12’,‘Cat12’, ‘Cat10’, ‘Cat10’, ‘Cat9’, ‘Cat9’, ‘Cat13’, ‘Cat13’, ‘Cat14’, ‘Cat14’, ‘Cat15’, ‘Cat15’, ‘Cat16’,‘Cat16’, ‘Cat17’]
y = [0.0, 0.0, 0.0, 0.0047, 0.0, 0.036, 0.0, 0.0, 0.12314, 0.02472495, 0.004,0.0, 0.013, 0.0, 0.0, 0.184, 0.056, 0.0186, 0.005928, 0.340, 0.20335, 0.0, 0.0, 0.2481, 0.12, 0.0, 0.0, 0.0201, 0.050, 0.0,0.0, 0.041, 0.0199, 0.0]
data = { “x”: x, “y”: y, }
df = pd.DataFrame(data)
box_plot = px.box(df, x=“x”, y=“y”, points=“all”, width=800, height=400) box_plot.update_yaxes(title=“Random numbers”, title_font=dict(size=18, family=‘Arial’), tickfont=dict(family=‘Arial’, size=18)) box_plot.update_xaxes(title=None, tickangle=45, title_font=dict(size=18, family=‘Arial’), tickfont=dict(family=‘Arial’, size=18), categoryorder=‘array’, categoryarray=[“Cat2”, “Cat1”, “Cat3”,“Cat4”, “Cat5”, “Cat6”, “Cat10”, “Cat11”, “Cat12”,“Cat9”, “Cat8”, “Cat7”, “Cat13”, “Cat14”, “Cat15”,“Cat16”, “Cat17”]) box_plot.update_layout(margin=dict(l = 40, r = 10, t = 10, b = 25), width=1100, height=400, font_family=“Arial”) box_plot.update_traces(boxmean=“sd”, selector=dict(type=‘box’)) box_plot.update_traces(pointpos=-2, selector=dict(type=‘box’)) box_plot.update_traces(marker_symbol=“circle-open”, selector=dict(type=‘box’)) box_plot.show()
pio.write_image(box_plot, r"Boxplot_minimal_work_ex.svg")
I tested first with only two categories, and the export file looked fine! But when I increase the number of categories, it makes the bad quality graph. I wonder if there is an influence from setting the width - so I tried to delete the width and heigth setting from the px.box expression but it gave same bad result.
image
svg
plotly
plotly-express
0 Answers
Your Answer