使用matplotlib中的换行符对齐图例标题(Centering a legend title with line breaks in matplotlib)
我使用以下代码来显示matplotlib的图例标题 :
import matplotlib.pyplot as plt # data all_x = [10,20,30] all_y = [[1,3], [1.5,2.9],[3,2]] # Plot plt.plot(all_x, all_y) # Add legend, title and axis labels plt.legend( [ 'Lag ' + str(lag) for lag in all_x], loc='lower right', title='hello hello hello \n world') plt.show()
正如您所看到的,“世界”并非居中。 我希望它居中,我可以通过手动添加空格来实现:
import matplotlib.pyplot as plt # data all_x = [10,20,30] all_y = [[1,3], [1.5,2.9],[3,2]] # Plot plt.plot(all_x, all_y) # Add legend, title and axis labels plt.legend( [ 'Lag ' + str(lag) for lag in all_x], loc='lower right', title='hello hello hello \n world') plt.show()
但这是一个麻烦的解决方案。
有没有更合适的方法来实现这一目标?
I use the following code to display a legend title with matplotlib:
import matplotlib.pyplot as plt # data all_x = [10,20,30] all_y = [[1,3], [1.5,2.9],[3,2]] # Plot plt.plot(all_x, all_y) # Add legend, title and axis labels plt.legend( [ 'Lag ' + str(lag) for lag in all_x], loc='lower right', title='hello hello hello \n world') plt.show()
As you can see, "world" is not centered. I would like it to be centered, I can achieve that by manually adding spaces:
import matplotlib.pyplot as plt # data all_x = [10,20,30] all_y = [[1,3], [1.5,2.9],[3,2]] # Plot plt.plot(all_x, all_y) # Add legend, title and axis labels plt.legend( [ 'Lag ' + str(lag) for lag in all_x], loc='lower right', title='hello hello hello \n world') plt.show()
but that's a cumbersome solution.
Is there any more proper way to achieve that?
原文:https://stackoverflow.com/questions/25356675
最满意答案
你为什么不在最后一行放一个普通的断点? 你已经知道str是空的。 如果你想仔细检查你的字符串是否为空,我会改用ASSERT。
如果你真的必须检查你的字符串,你必须检查CString中的
m_pszData
,所以你的条件如下所示:str.m_pszData[0] == '\0'
Why don't you just put a normal breakpoint on the last line? You already know str is empty. If you want to double check whether your string is empty, I would use an ASSERT instead.
If you really have to check your string, you have to check
m_pszData
in your CString, so your condition looks like this:str.m_pszData[0] == '\0'
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
有什么办法可以在非基元类型上设置条件断点? 是的,一种方法是将非原始类型转换为原始类型,在您的情况下转换为char* ,然后使用strcmp比较字符串。 condition 1 strcmp(myObject->myStringVar.c_str(),"foo") == 0 Is there any way I could set a conditional breakpoint on non-primitive types? Yes, one way to do it is to convert non ...
-
一种转换方法是这样的: CString str; str = "Hello"; LPCSTR szTemp = (LPCSTR)(LPCTSTR)str; One method of conversion is like this: CString str; str = "Hello"; LPCSTR szTemp = (LPCSTR)(LPCTSTR)str;
-
Visual Studio条件断点缓慢的解决方案或工作区(Solutions or workaound for Visual Studio conditional breakpoint slowness)[2024-03-02]
为什么不停止使用有条件的断点并在代码调试之前更改代码。 所以代码会变成这样: int dummyx = 0; if (quote.Time > new DateTime (2014,2,4,3,59,0 ) { dummyx++; // put normal break point here! } 这将运行得更快。 Evidently conditional break points being very slow is a known issue. See: http://www.bing.com/ ... -
CString的最大长度(CString maximum length)[2023-11-19]
直到您的可用内存或INT_MAX-1(以较少者为准)。 Up to your available memory or INT_MAX-1 (whichever is less). -
VS中的条件断点(Conditional breakpoint in VS)[2022-09-02]
当然你必须设置breakpont并右键单击它。 从上下文菜单中选择“Condition ...”并在那里键入变量名称。 并选择“是真的”。 在条件字段中,您可以编写if语句中的表达式。 Sure You have to set breakpont and right click on it. From context menu choose "Condition..." and type your variable name there. And select "Is true". In condition ... -
无法保证当VARIANT.vt == VT_EMPTY或VARIANT.vt == VT_NULL时,其余字段归零。 您的解决方法功能是正确的方法。 我只建议使用const COleVariant&作为参数来避免不必要的COleVariant对象复制。 There is simply no guarantee that when VARIANT.vt == VT_EMPTY or VARIANT.vt == VT_NULL the rest of fields are zeroed. You workaro ...
-
如何设置条件断点来测试CString变量是否为空(How to put a conditional breakpoint to test if a CString variable is empty)[2023-12-07]
你为什么不在最后一行放一个普通的断点? 你已经知道str是空的。 如果你想仔细检查你的字符串是否为空,我会改用ASSERT。 如果你真的必须检查你的字符串,你必须检查CString中的m_pszData ,所以你的条件如下所示: str.m_pszData[0] == '\0' Why don't you just put a normal breakpoint on the last line? You already know str is empty. If you want to double c ... -
您可以尝试将参数作为字段放在本地类中。 static Test foo(final String arg) { return new Test() { private final String localArg = arg; @Override void bar() { // I want to set a breakpoint here with the condition "arg==null" ...