为什么从Mathematica导出的BarChart图形具有像素化文本?有解决方法吗? [英] Why do BarChart graphics exported from Mathematica have pixelated text? Is there a workaround?

查看:58
本文介绍了为什么从Mathematica导出的BarChart图形具有像素化文本?有解决方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在炫耀我的花哨的新图形格式给同事,但我们发现基于BarChart的图形导出为EMF,WMF,PDF等时具有锯齿状文本.基于ListLinePlotDateListPlot等的折线图不存在此问题./p>

Rasterize的简短版本-自动执行每个Export函数(这是针对最终用户的应用程序,因此不能期望他们自己摆弄它),是否有解决方法?令人惊讶的是,文档说:

由于EMF支持矢量图形,因此在导出到EMF时不会对字体进行栅格化.

编辑(如果相关),则使用的字体为Arial.这应该给您一些非常接近该图的信息,除了tickgrid业务之外,该业务涉及的自定义功能比一个人真正想了解的要多.

 SetOptions[BarChart,Background->None, BaseStyle -> {20, FontFamily -> Rfont}, 
Frame -> True,  FrameTicksStyle -> {{Directive[20, 20], 20}, {20, 20}}, 
 FrameStyle -> 
 Directive[AbsoluteThickness[0.9], FontFamily -> Rfont, Black], 
 AspectRatio -> 14./19., PlotRangePadding -> None, Ticks -> None,
 ChartBaseStyle -> EdgeForm[None], GridLinesStyle->Directive[GrayLevel[0.7],  
 AbsoluteThickness[0.9]], GridLines -> {None, Automatic},
 ImageSize -> 672, ImageMargins -> {{0, 0}, {0, 3}}, 
ImagePadding -> {{66, 66}, {All, 1}}
]

SetOptions[ListPlot,Background->None,BaseStyle -> {20, FontFamily -> Rfont,  
 AbsolutePointSize[6]}, Frame -> True, 
 FrameStyle -> Directive[AbsoluteThickness[0.9], FontFamily -> "Arial", Black], 
 FrameTicksStyle -> {{Directive[20, 20], 20}, {20, 20}}, 
 AspectRatio -> 14./19., GridLinesStyle->Directive[GrayLevel[0.7],
 AbsoluteThickness[0.9]], GridLines -> {None, Automatic},PlotRangePadding->None,
  ImageSize -> 672, ImageMargins -> {{0, 0}, {0, 3}}, 
  ImagePadding -> {{66, 66}, {All, 1}}
 ];

areaharvested = {0.25, 1.25, 0.3, -0.1, -0.5, -0.5, -0.5, 0.25, 0.4};
yield = {3.25, 1.1, 2.6, 3., 2., -0.3, 2., 1.5, 1.2};
totalgrainprod = areaharvested + yield;


exgraph = Show[BarChart[Transpose@{areaharvested, yield}, ChartLayout -> "Stacked",
ChartStyle -> {Orange, Green}, PlotRange ->{{8.5, 9.5}, {-1, 4.}},   
 PlotRangePadding -> None,
 FrameTicks ->{{myTickGrid[-1, 4, 1, "%"], myTickGrid[-1, 4, 1, "%"]},
  {myBarChartTicks[{"67-71", "77-81", "87-91", "97-01", "07-11"}, 9], None}}],
ListPlot[totalgrainprod, PlotStyle -> AbsolutePointSize[13]]]    

Export["exgraph.emf", exgraph]
 

解决方案

更新

很多年后,Wolfram修复了问题.

Export[stringtouse, 
     DeleteCases[ obj /. {_Opacity, p_Point} :> 
     {PointSize[0], p}, _Opacity, Infinity], opts]

我将其绑定到这样的小辅助函数中.

ExportEMFNicely[pathorfile_String, obj_, opts:OptionsPattern[{Export}]]:=
With[{stringtouse = If[ToLowerCase[StringTake[pathorfile,-4]]===".emf", 
 pathorfile, pathorfile<>".emf"]},
Export[stringtouse, 
 DeleteCases[ obj /. {_Opacity, p_Point} :> 
  {PointSize[0], p}, _Opacity, Infinity], opts] ]

这将产生矢量EMF,而无需Magnify或使用ImageResolution骇客.

原始答案

Wolfram的支持又回到了我身边.简短的答案是,这是Mathematica中的错误,因此他们建议您使用其他格式或Rasterize

感谢您的电子邮件.与出口质量有关的问题 来自Mathematica的图像在过去已经报道过,而我们 开发人员正在研究这些.但是,我已经单独提交了 代表您举报.我还包括了您的联系信息 这样一来,您就可以收到通知.

同时,您可以尝试的另一种方法是光栅化 具有适当分辨率的图形,然后导出到EMF.

Rasterize[graphic, ImageResolution-> XXX]

您还可以尝试导出为其他Windows格式,例如RTF.

编辑

此后,我得出结论,可以使用Export命令中的ImageResolution很高的值来解决此问题(至少在v 8.0.4和v 9.0.1中).

bc = BarChart[RandomInteger[{1, 20}, {15}], Frame -> True, 
  FrameStyle -> AbsoluteThickness[1], PlotRangePadding -> 0, 
  PlotRange -> {0, 20}, 
  BaseStyle -> {FontFamily -> "Arial", FontSize -> 16}, 
  LabelingFunction -> None]

Export["testbarchart.emf", bc, ImageResolution -> 2000]

ImageResolution设置为1300或更高会得到矢量格式的文本和50k EMF文件.但是,将其设置为1000会导致高分辨率栅格占用48 Mb!据我所知,这种行为是无证的.它似乎还会出现刻度线问题,因为仅当您使用更复杂的TicksFrameTicks等语法明确设置它们的长度时,它们才会出现(请参阅fancy new graph formats to colleagues, but we have discovered that graphics based on BarChart have jagged text when exported as EMF, WMF, PDF etc. Line graphs based on ListLinePlot, DateListPlot etc do not have this problem.

Short of Rasterize-ing every Export function automatically (it's for an application for end-users so they can't be expected to fiddle with it themselves), is there a workaround? It's a surprise because the documentation says:

Since EMF supports vector graphics, fonts are not rasterized when exporting to EMF.

EDIT If it's relevant, font used is Arial. This should give you something very close to the graph, except for the tickgrid business, which involves more custom functions than one would really want to wade through.

SetOptions[BarChart,Background->None, BaseStyle -> {20, FontFamily -> Rfont}, 
Frame -> True,  FrameTicksStyle -> {{Directive[20, 20], 20}, {20, 20}}, 
 FrameStyle -> 
 Directive[AbsoluteThickness[0.9], FontFamily -> Rfont, Black], 
 AspectRatio -> 14./19., PlotRangePadding -> None, Ticks -> None,
 ChartBaseStyle -> EdgeForm[None], GridLinesStyle->Directive[GrayLevel[0.7],  
 AbsoluteThickness[0.9]], GridLines -> {None, Automatic},
 ImageSize -> 672, ImageMargins -> {{0, 0}, {0, 3}}, 
ImagePadding -> {{66, 66}, {All, 1}}
]

SetOptions[ListPlot,Background->None,BaseStyle -> {20, FontFamily -> Rfont,  
 AbsolutePointSize[6]}, Frame -> True, 
 FrameStyle -> Directive[AbsoluteThickness[0.9], FontFamily -> "Arial", Black], 
 FrameTicksStyle -> {{Directive[20, 20], 20}, {20, 20}}, 
 AspectRatio -> 14./19., GridLinesStyle->Directive[GrayLevel[0.7],
 AbsoluteThickness[0.9]], GridLines -> {None, Automatic},PlotRangePadding->None,
  ImageSize -> 672, ImageMargins -> {{0, 0}, {0, 3}}, 
  ImagePadding -> {{66, 66}, {All, 1}}
 ];

areaharvested = {0.25, 1.25, 0.3, -0.1, -0.5, -0.5, -0.5, 0.25, 0.4};
yield = {3.25, 1.1, 2.6, 3., 2., -0.3, 2., 1.5, 1.2};
totalgrainprod = areaharvested + yield;


exgraph = Show[BarChart[Transpose@{areaharvested, yield}, ChartLayout -> "Stacked",
ChartStyle -> {Orange, Green}, PlotRange ->{{8.5, 9.5}, {-1, 4.}},   
 PlotRangePadding -> None,
 FrameTicks ->{{myTickGrid[-1, 4, 1, "%"], myTickGrid[-1, 4, 1, "%"]},
  {myBarChartTicks[{"67-71", "77-81", "87-91", "97-01", "07-11"}, 9], None}}],
ListPlot[totalgrainprod, PlotStyle -> AbsolutePointSize[13]]]    

Export["exgraph.emf", exgraph]

解决方案

UPDATE

Many years later, Wolfram came back with a fix.

Export[stringtouse, 
     DeleteCases[ obj /. {_Opacity, p_Point} :> 
     {PointSize[0], p}, _Opacity, Infinity], opts]

I bound this up into a little helper function like this.

ExportEMFNicely[pathorfile_String, obj_, opts:OptionsPattern[{Export}]]:=
With[{stringtouse = If[ToLowerCase[StringTake[pathorfile,-4]]===".emf", 
 pathorfile, pathorfile<>".emf"]},
Export[stringtouse, 
 DeleteCases[ obj /. {_Opacity, p_Point} :> 
  {PointSize[0], p}, _Opacity, Infinity], opts] ]

This produces vector EMFs without any need to Magnify or use ImageResolution hacks.

ORIGINAL ANSWER

Wolfram support got back to me. Short answer is that it is a bug in Mathematica and they recommend either using another format or Rasterize

Thank you for your email. Issues relating to the quality of exported images from Mathematica have been reported in the past and our developers are looking into these. I have however filed a separate report on your behalf. I have also included your contact information so you can be notified when this is resolved.

In the meantime, the other option that you can try is to rasterize the graphic with an appropriate resolution before exporting to EMF.

Rasterize[graphic, ImageResolution-> XXX]

You could also try exporting to other Windows formats like RTF.

EDIT

I have since worked out that you can work around this issue (at least in v 8.0.4 and v 9.0.1) using a very high value for ImageResolution in the Export command.

bc = BarChart[RandomInteger[{1, 20}, {15}], Frame -> True, 
  FrameStyle -> AbsoluteThickness[1], PlotRangePadding -> 0, 
  PlotRange -> {0, 20}, 
  BaseStyle -> {FontFamily -> "Arial", FontSize -> 16}, 
  LabelingFunction -> None]

Export["testbarchart.emf", bc, ImageResolution -> 2000]

Setting ImageResolution to 1300 or higher results in vector-format text and a 50k EMF file. However setting it to 1000 results in a high-resolution raster taking up 48 Mb! This behaviour is, as far as I know, undocumented. It also seems to create problems with tick marks, in that they only show up if you explicitly set their lengths using the more complex syntax for Ticks, FrameTicks etc (see documentation.)

One caveat to this fix is that Mathematica still thinks that it needs as much memory to create this smaller, vector-based EMF file as it would to create the high-resolution bitmap. So it will sometimes complain about not having enough memory and you will need to quit out of some other applications. It doesn't actually need all that memory to create the vector EMF. In my experiments, anything 1300 or above will work to trigger the vector export, while 1200 and below will generate a high-resolution, enormous bitmap.

这篇关于为什么从Mathematica导出的BarChart图形具有像素化文本?有解决方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆