我正在尝试绘制带有长 x 标签字符串的图表。这篇文章描述了一个类似的问题(如何使用 自动换行 y 轴标签tight_layout),但对我来说不起作用,因为tight_layout改变了比例。另一篇文章描述了一种无法实现轴标签自动换行的方法 - 该包textwrap需要指定要换行的字符数。
我的代码:
import matplotlib.pyplot as plt
plt.figure()
plt.bar([0,1,2,3],[0,1,4,9])
labels = ['superdupersuperduperlonglabel0','superdupersuperduperlonglabel1',
'superdupersuperduperlonglabel2','superdupersuperduperlonglabel3']
plt.xticks([0,1,2,3],labels, wrap = True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
这是结果图,x 标签重叠:

根据 matplotlib 文档:
文本属性可用于控制标签的外观。
但wrap = True似乎没有正确包装 x 标签。有什么建议吗?
我编写了读取两个字符串的代码,然后将它们比较相似的单词。然后产生一个带有数据的表格。
我的问题是它不断分裂为两个。我需要对此进行纠正,以便能够将其合并到HTML中。我将不胜感激,在此先感谢您!:)
我也尝试只打印第一行。
完整代码:
import string
from os import path
import pandas as pd
pd.set_option('display.max_columns', None) #prevents trailing elipses
pd.set_option('display.max_rows', None)
import os.path
BASE = os.path.dirname(os.path.abspath(__file__))
file1 = open(os.path.join(BASE, "samp.txt"))
sampInput=file1.read().replace('\n', '')
file2 = open(os.path.join(BASE, "ref.txt"))
refInput=file2.read().replace('\n', '')
sampArray = [word.strip(string.punctuation) for word in sampInput.split()]
refArray = [word.strip(string.punctuation) for word in refInput.split()]
out=pd.DataFrame(index=sampArray,columns=refArray)
for i in range(0, out.shape[0]): #from 0 to total number of rows
for word in refArray: #for each word in the samplearray
df1 = out.iloc[0, 0:16].copy()
top …Run Code Online (Sandbox Code Playgroud) 这是一件奇怪的事情,我只注意到一台特定的计算机。如果我让 Jupyter Notebook 页面不活动一段时间(不关闭浏览器页面或让计算机进入睡眠状态)然后回到它,内核将看起来完全没有响应;但它也不会说“内核已死”。从 Jupyter Notebook 重新启动内核没有任何作用,因此我总是不得不关闭运行 Jupyter Notebook 的命令窗口,不用说所有变量都丢失了。
每当发生这种情况时,Jupyter Notebook 页面上“死亡”后的任何活动都不会记录在运行它的命令窗口中。
尝试在 GitHub 上搜索,这可能是一个更合适的渠道,但没有运气。我很乐意提供更多信息。谢谢!