增加matplotlib字符串的字体大小

时间:2017-12-12 21:55:45

标签: python matplotlib styling rc

我希望增加绘图标题字体大小,将其与例如@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_client_app); //Copy and paste this line to onClick methods regarding the edit text dialog // textentry = (EditText)findViewById(R.id.entrybox); // Button year = (Button)findViewById(R.id.Year); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options, menu); return true; } //Dialog Box for selecting View books in Options Menu public Dialog ViewBookOptions() { AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = this.getLayoutInflater(); View v = inflater.inflate(R.layout.view_books, null); builder.setView(v); final AlertDialog dialog = builder.create(); return dialog; } //Dialog boxes for selecting Year, Author, or Title in the ViewBook Dialog Box public Dialog ViewByYear(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = this.getLayoutInflater(); View v = inflater.inflate(R.layout.text_entry, null); builder.setView(v); final EditText entrybox = (EditText) v.findViewById(R.id.entrybox); builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String entry = entrybox.getText().toString().toLowerCase(); //This is Where You Will Populate The ListView With Database Stuff } }); builder.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog dialog = builder.create(); return dialog; } //Quick Toast for checking stuff public void toast(String string){ Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); } //All of my button OnClick Methods public void allbooks(View view){ toast("You clicked All books"); } public void year(View view){ toast("Dialog should open and EditText should change hint text"); EditText textentry = (EditText)findViewById(R.id.entrybox); textentry.setHint("Enter the Year of the Book(s)"); Dialog dialog = ViewByYear(); dialog.show(); } // The Options Menu @Override public boolean onOptionsItemSelected (MenuItem item) { switch (item.getItemId()) { case R.id.option1: //Change to View Book Options Dialog dialog = ViewBookOptions(); dialog.show(); return true; case R.id.option2: return true; case R.id.option3: return true; case R.id.option4: return true; case R.id.option5: return true; default: return super.onOptionsItemSelected(item); } } medium

我的图表是从matplotlibrc large文件中获取基本字体大小和修饰符。 这通常表达如下:

.conf

现在,对于我的一个图,我想增加标题字体大小,特别是。 如果这是一个数字(例如线宽),我可以做类似的事情:

font.size : 8
axes.titlesize : medium

但这不会那么容易。 当然,我可以使用字典或一系列from matplotlib import rcParams newsize = rcParams['lines.linewidth']*2 语句 - 但我在想matplotlib,知道如何解释这些字符串,也许还有一些函数来增加它们。你知道这样的事吗?

编辑:有人建议这与其他一些问题重复。这个问题专门询问有关增加“字符串字体大小”的问题(正如我在问题标题中所写的那样)。这意味着手动将字体大小设置为某个整数不是一个选项(除非有办法将matplotlibrc的elifsmall转换为整数 - 这也是此问题的独特之处。)

1 个答案:

答案 0 :(得分:1)

所以问题似乎是:给定"axes.titlesize" rcParam,如何将标题大小设置为下一个更高的相对大小规范。

解决方案肯定取决于"axes.titlesize"中可能存在的值。例如,我们已经知道那些值只能是 'xx-small','x-small','small','medium','large','x-large'之一,解决方案相当容易。人们可以在所有项目列表中选择下一个可能的项目。

import matplotlib.pyplot as plt
sizes = ['xx-small','x-small','small','medium','large','x-large','xx-large']
plt.rcParams["axes.titlesize"] = sizes[sizes.index(plt.rcParams["axes.titlesize"])+1]

通用的一般情况,其中"axes.titlesize"可以是数字或任何有效大小的字符串,将通过以下方式处理:

import matplotlib.pyplot as plt
sizes = ['xx-small','x-small','small','medium','large','x-large','xx-large']
cs = plt.rcParams["axes.titlesize"]
try:
    plt.rcParams["axes.titlesize"] = float(cs)*1.2
except:
    if cs in ["larger", "smaller"]:
        cs = cs[:5]
    if cs in sizes[:-1]:    
        plt.rcParams["axes.titlesize"] = sizes[sizes.index(cs)+1]
    elif cs == 'xx-large':
        plt.rcParams["axes.titlesize"] =  plt.rcParams["font.size"]*1.2**4