python-使用3个图在matplotlib中的x轴上对齐/旋转文本标签
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-使用3个图在matplotlib中的x轴上对齐/旋转文本标签,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3752字,纯文字阅读大概需要6分钟。
内容图文
![python-使用3个图在matplotlib中的x轴上对齐/旋转文本标签](/upload/InfoBanner/zyjiaocheng/661/ee7ced1a414e4cc1bdec8a0e50d543ff.jpg)
如何在此处将文本标签与图中的x标记对齐?我正在使用host.set_xticklabels(labels,rotation =’vertical’),但这似乎不起作用.
我的标签是句子,有些标签可能比其他标签小/大,例如“木乃伊返回第2部分”-如何在x轴下方填充空格以适应此问题?
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
path='/home/project/df.csv'
df=pd.read_csv(path,sep=',',header='infer')
xs =range(0,101)
ys =list(df['B'].ix[0:100])
zs=list(df['C'].ix[0:100])
ws=list(df['D'].ix[0:100])
if 1:
host = host_subplot(111, axes_class=AA.Axes)
p1, = host.plot(xs, ys, label='B')
plt.subplots_adjust(right=0.75)
par1 = host.twinx()
par2 = host.twinx()
offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
axes=par2,
offset=(offset, 0))
par2.axis["right"].toggle(all=True)
host.set_xlim(min(xs), max(xs))
host.set_ylim(100, max(ys))
host.set_xlabel("A")
host.set_ylabel("B")
par1.set_ylabel("C")
par2.set_ylabel("D")
p2, = par1.plot(xs, ws, label='C',color='red')
p3, = par2.plot(xs, zs, label='D',color='green')
for tl in par1.get_yticklabels():
tl.set_color('red')
for tl in par2.get_yticklabels():
tl.set_color('green')
for tl in par1.get_yticklabels():
tl.set_color('r')
for tl in par2.get_yticklabels():
tl.set_color('g')
host.legend()
host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())
start, end, stepsize=0,len(df)-1,3
host.xaxis.set_ticks(np.arange(start, end, stepsize))
labels=list(df['A'])[0::stepsize]
host.set_xticklabels(labels,rotation='vertical')
plt.tight_layout()
plt.draw()
plt.show()
我需要对齐图片中的“黑色”质量.
我在这里的示例中尝试了建议python rotate values on xaxis to not overlap-plt.xticks(x,labels,rotation =’vertical’),等等,但是没有用.
编辑-:
从下面的Kazemakaze反馈中,这是我尝试的-:
f,host = plt.subplots()
#host = host_subplot(111,axes_class = AA.Axes)
但是我也必须修改其余的代码.您能提供一个适用于Twinx轴的示例吗?
我正在将代码修改为该Secondary axis with twinx(): how to add to legend?,并且旋转现在可以正常工作,但是在辅助轴上需要一些指针.
解决方法:
这是我解决的方法-不旋转的问题确实与host_subplot有关,并且在使用plt.subplot时可以正常工作
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
path='/home/project/df.csv'
df=pd.read_csv(path,sep=',',header='infer')
xs =range(0,101)
ys =list(df['B'].ix[0:100])
zs=list(df['C'].ix[0:100])
ws=list(df['D'].ix[0:100])
def make_patch_spines_invisible(ax):
ax.set_frame_on(True)
ax.patch.set_visible(False)
for sp in ax.spines.itervalues():
sp.set_visible(False)
def three_way_plot(xs,ys,ws,zs,category):
fig, host = plt.subplots()
fig.subplots_adjust(right=0.6)
par1 = host.twinx()
par2 = host.twinx()
par2.spines["right"].set_position(("axes", 1.1))
p1, = host.plot(xs, ys, "blue", linewidth=2.0, label="B")
p2, = par1.plot(xs, ws, "r-", label="C")
p3, = par2.plot(xs, zs, "g-", label="D")
host.set_xlim(min(xs), max(xs))
host.set_ylim(min(ys), max(ys) + 200)
par1.set_ylim(min(ws), max(ws) + 200)
par2.set_ylim(min(zs), max(zs))
host.set_xlabel("A", fontsize=14)
host.set_ylabel("B", fontsize=14)
par1.set_ylabel("C", fontsize=14)
par2.set_ylabel("D", fontsize=14)
host.yaxis.label.set_color(p1.get_color())
par1.yaxis.label.set_color(p2.get_color())
par2.yaxis.label.set_color(p3.get_color())
lines = [p1, p2, p3]
labels = ["mary had a little lamb","The mummy returns","Die another day","Welcome back"]*25
start, end, step_size = 0, len(df) - 1, 4
host.set_xticks(np.arange(start, end, step_size))
host.set_xticklabels(labels, rotation='vertical', fontsize=10)
host.legend(lines, [l.get_label() for l in lines])
plt.tight_layout()
plt.show()
three_way_plot(xs,ys,ws,zs,"category")
当然,我掩盖了数据的True标签.
内容总结
以上是互联网集市为您收集整理的python-使用3个图在matplotlib中的x轴上对齐/旋转文本标签全部内容,希望文章能够帮你解决python-使用3个图在matplotlib中的x轴上对齐/旋转文本标签所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。