Matplotlib:Times New Roman看起来很大胆

时间:2015-11-27 10:58:10

标签: python matplotlib

出于某种原因,在我的import com.google.android.gms.maps.model.LatLng; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class DirectionsJSONParser { /** Receives a JSONObject and returns a list of lists containing latitude and longitude */ public List<List<HashMap<String,String>>> parse(JSONObject jObject){ List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String,String>>>() ; JSONArray jRoutes = null; JSONArray jLegs = null; JSONArray jSteps = null; try { jRoutes = jObject.getJSONArray("routes"); /** Traversing all routes */ for(int i=0;i<jRoutes.length();i++){ jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs"); List path = new ArrayList<HashMap<String, String>>(); /** Traversing all legs */ for(int j=0;j<jLegs.length();j++){ jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps"); /** Traversing all steps */ for(int k=0;k<jSteps.length();k++){ String polyline = ""; polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points"); List list = decodePoly(polyline); /** Traversing all points */ for(int l=0;l <list.size();l++){ HashMap<String, String> hm = new HashMap<String, String>(); hm.put("lat", Double.toString(((LatLng)list.get(l)).latitude) ); hm.put("lng", Double.toString(((LatLng)list.get(l)).longitude) ); path.add(hm); } } routes.add(path); } } } catch (JSONException e) { e.printStackTrace(); }catch (Exception e){ } return routes; } /** * Method to decode polyline points * Courtesy : http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java * */ private List decodePoly(String encoded) { List poly = new ArrayList(); int index = 0, len = encoded.length(); int lat = 0, lng = 0; while (index < len) { int b, shift = 0, result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lat += dlat; shift = 0; result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lng += dlng; LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5))); poly.add(p); } return poly; } } 图中使用Times New Roman时,它显得大胆。其他字体都可以。

这是一个最小的例子和结果(在Word文档中,与我期望的Times New Roman看起来相似)。

mpl

使用包含

import matplotlib as mpl import matplotlib.pyplot as plt with plt.style.context('word'): fig = plt.figure(1, figsize=(3.4, 2.1)) ax1 = plt.subplot(111) ax1.plot([1,2,3,4,5], '+--') ax1.text(0.5, 3.5, r"Brown $\alpha + 12 \sum_ix$") ax1.text(0.5, 3, r"1.0 2.0") ax1.set_xlabel('normal 1.0 and math $1.0$') ax1.set_ylabel('Times New Roman') plt.tight_layout() fig.savefig('word.pdf') 样式表
word

该图表包含在文档中,其实际大小(3.4&#39;&#39; by 2.1&#39;&#39;)。

正确找到了字体,它也在数学模式下工作(参见图中的alpha)。看起来这很大胆......

enter image description here

8 个答案:

答案 0 :(得分:11)

我知道这个问题已经很老了,但它仍然是一个问题,至少在我的Mac上是这样。我找到了一个非常简单的解决方案,由azag0在github上发布

del matplotlib.font_manager.weight_dict['roman']
matplotlib.font_manager._rebuild()

https://github.com/matplotlib/matplotlib/issues/5574

答案 1 :(得分:3)

深入研究更多细节我发现错误是真实的,而mpl实际上是选择Times New Roman Bold字体。

font_manger.py中的字体选择算法根据系列,变体,重量等对其找到的每种字体赋予权重(第1290行左右)。 &#34;名称&#34;来自Times New Roman Bold.ttf的只是“新时代的罗马”。这可能有意义,但重量是500,与常规字体相同:

<Font 'Times New Roman' (Times New Roman Bold.ttf) normal normal 500 normal> with score 0.1
<Font 'Times New Roman' (Times New Roman.ttf) normal normal 500 normal> with score 0.1

在我的Mac和Linux设置中,首先遇到粗体,然后由代码

选择
 if score < best_score:
     best_score = score
     best_font = font

我的脏补丁是用<替换<= ...

答案 2 :(得分:1)

当matlibplot从2.2.3更新到3.1.0时,我最近遇到了这个问题。 我在这里和其他网站上尝试了很多答案,但是字体“ times new roman”仍然显得粗体。但是我记得上次仍然是2.2.3时,字体看起来很正常。所以我只是用2.2.3中的font_manager.py替换了3.1.0中的font_manager.py:

将font_manager.py复制到.. \ anaconda \ pkgs \ matplotlib-2.2.3-py37hd159220_0 \ Lib \ site-packages \ matplotlib

并粘贴到此处:.. \ anaconda \ pkgs \ matplotlib-3.1.0-py37hc8f65d3_0 \ Lib \ site-packages \ matplotlib

希望对您有帮助。

答案 3 :(得分:1)

如前所述,字体选择算法选择Time New Roman系列中的第一种字体,该字体有四个不同的文件(粗体,斜体,斜体和常规)。因此,步骤将是:

  1. 转到C:\Windows\Fonts\Times New Roman
  2. 右键单击→排序方式→降序(顺序将更改,因此Times New Roman Regular将以第一个结束,并且该字体将是算法选择的字体)。

答案 4 :(得分:0)

删除var array2 = ['apple', 'banana', 'kiwi']; var cond2 = true; for(var idx = 0; idx < array2.length; idx++) { cond2 = cond2 && (value.indexOf(array2[idx]) == -1); if(!cond2) { // its already false, no need to iterate break; } } 解决了我的问题。

答案 5 :(得分:0)

我通过删除matplotlib / mpl-data / fonts中的timesbd.tff解决了这个问题。而且我不确定是否需要删除timesbi.tff和timesi.tff,但是我做到了。

答案 6 :(得分:0)

如上所述,mpls中的Times New Roman字体的默认字体权重很高(声称是500以上,我会用他们的话说。)

最简单的解决方法是使用fontweight自行设置权重。

plt.xlabel('t',fontsize=32, fontname = 'Times New Roman', fontweight = 250)

这对我有用,看起来很正常。

答案 7 :(得分:0)

在Windows 10,Jupyter Notebook上使用python 3.7,未安装Anaconda。

如上所述,仅调整字体顺序C:\Windows\Fonts\Times New Roman在这里不起作用。

C:\User\Name\.matplolib,找到一个名为fontlist-v300的JSON文件(文本文件)。

删除

{
      "fname": "C:\\WINDOWS\\Fonts\\timesbd.ttf",
      "name": "Times New Roman",
      "style": "normal",
      "variant": "normal",
      "weight": "roman",
      "stretch": "normal",
      "size": "scalable",
      "__class__": "FontEntry"
}
文本文件中的

对我有用。

但这不是一个好方法。