python坐标值倾斜防止重叠

python将横坐标轴的值倾斜一定角度,以避免重叠

import matplotlib.pyplot as plt

"""
a1 = "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.17979811 0.0 0.19455098 0.22128937 0.20096813 0.20339341 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0"
aa1 = "the so-called grandmother hypothesis  based on studies of african hunter-gatherer groups  suggests that infertile women are vital for successful child-rearing despite being unable to produce children themselves "
a2 = "0.0 0.0 0.0 0.0 0.0 0.18585661 0.0 0.0 0.19287187 0.21022634 0.20847885 0.20256634 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0"
aa2 = "the grandmother hypothesis suggests that infertile women are very important for raising children"

a1 = "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.19144899 0.22170746 0.20416436 0.19487111 0.18780807 0.0"
aa1 = "trade between china and india is expected to touch $20 bn this year and chinese investors are now rushing into india - lured by the country s growing wealth"
a2 = "0.0 0.0 0.0 0.0 0.0 0.0 0.19154933 0.0 0.21717739 0.20123896 0.19672251 0.19331184 0.0 0.0"
aa2 = "there is a profitable trade between china and india"
"""
a1 = "0.18417412 0.20065087 0.21265925 0.23537883 0.1671369 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0"
aa1 = "successful plaintiffs recovered punitive damages in texas discrimination cases 53  of the time"
a2 = "0.21300969 0.21517463 0.0 0.18324433 0.20611063 0.0 0.0 0.0 0.0 0.18246076 0.0"
aa2 = "legal costs to recover punitive damages are a deductible business expense "

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

y1 = [float(r) for r in a1.split()]
x1 = [str(i)+"_"+v for i,v in enumerate(aa1.split())]

while len(x1)<len(y1):
    x1.append(str(len(x1))+"_.")

for tick in ax1.get_xticklabels():  # 将横坐标倾斜30度,纵坐标可用相同方法
    tick.set_rotation(30)
ax1.plot(x1, y1, 'go--')

y2 = [float(r) for r in a2.split()]
x2 = [str(i)+"_"+v for i,v in enumerate(aa2.split())]
while len(x2)<len(y2):
    x2.append(str(len(x2))+"_.")


ax2.plot(x2, y2, 'ro--')
for tick in ax2.get_xticklabels():
    tick.set_rotation(30)

plt.show()

效果图如下:
xiaoguotu

### 回答1: 可以使用Matplotlib库中的xticks()方法来设置横坐标倾斜角度。具体做法是,使用plt.xticks()函设置xticks,然后使用setp()方法设置xticks的属性,包括rotation(倾斜角度)和horizontalalignment(对齐方式)等。例如: ``` import matplotlib.pyplot as plt # 生成据 x = [1, 2, 3, 4, 5] y = [10, 8, 6, 4, 2] # 绘制图形 plt.plot(x, y) # 设置x轴刻度倾斜角度为45度 plt.xticks(rotation=45, horizontalalignment='right') # 显示图形 plt.show() ``` 在上述代码中,plt.xticks()函用于设置x轴刻度,rotation=45设置x轴刻度倾斜角度为45度,horizontalalignment='right'表示右对齐。 ### 回答2: 在Python中,可以使用Matplotlib库进行绘图。要让横坐标倾斜,你可以通过以下步骤实现: 1. 首先,需要导入Matplotlib库的pyplot模块,该模块包含了绘图的主要函和工具。 2. 创建一个图形对象并设置图形的大小、标题等属性。例如,使用`plt.figure()`创建一个新的图形对象。 3. 绘制图形的据并指定横坐标和纵坐标的值。例如,使用`plt.plot(x, y)`绘制据曲线,其中`x`是包含横坐标值的列表或组,`y`是包含纵坐标值的列表或组。 4. 使用`plt.xticks(rotation=45)`给横坐标标签设置倾斜角度。`rotation=45`表示将标签倾斜45度,你可以根据需要调整这个角度。 5. 可以选择性地添加其他图形元素,如图例、标签等。 6. 最后,使用`plt.show()`显示绘制的图形。 下面是一个简单的例子,演示如何让横坐标倾斜45度: ```python import matplotlib.pyplot as plt # 创建一个新的图形对象 plt.figure() # 定义横坐标和纵坐标的值 x = [1, 2, 3, 4, 5] y = [10, 20, 15, 25, 30] # 绘制据曲线 plt.plot(x, y) # 设置横坐标标签倾斜角度为45度 plt.xticks(rotation=45) # 显示图形 plt.show() ``` 以上代码会绘制一个具有倾斜横坐标的简单折线图,你可以根据自己的需求调整和添加其他图形元素。希望对你有所帮助! ### 回答3: 在Python中使用Matplotlib库中的plot函进行绘图时,可以通过设置横坐标倾斜角度来改变横坐标的显示方式。 首先,导入必要的库和模块: ```python import matplotlib.pyplot as plt ``` 然后,创建一个带有据的图表: ```python x = [1, 2, 3, 4, 5] y = [10, 20, 30, 40, 50] plt.plot(x, y) ``` 接下来,使用`xticks`函来设置横坐标的显示方式。通过设置参`rotation`来调整倾斜角度,使用`ha`参来调整标签的水平对齐方式。例如,设置倾斜角度为45度,水平对齐方式为右对齐: ```python plt.xticks(rotation=45, ha='right') ``` 最后,通过`show`方法显示图表: ```python plt.show() ``` 这样就能够实现横坐标倾斜的效果。根据需要,可以调整`rotation`参值来改变倾斜角度,或者使用其他的水平对齐方式,如`'left'`、`'center'`等。 完整的代码示例如下: ```python import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [10, 20, 30, 40, 50] plt.plot(x, y) plt.xticks(rotation=45, ha='right') plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值