python – 用于子图的plt.setp替代或如何在x轴上为子图设置文本旋转
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 用于子图的plt.setp替代或如何在x轴上为子图设置文本旋转,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7060字,纯文字阅读大概需要11分钟。
内容图文
![python – 用于子图的plt.setp替代或如何在x轴上为子图设置文本旋转](/upload/InfoBanner/zyjiaocheng/696/3d216b1a18f544319081279e3b31ce8e.jpg)
我有这个代码,我可以控制以下属性:x轴范围,标题,xlabel,ylabel,图例,网格,x标签上文字的旋转:
#!/usr/bin/python
import datetime
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib.dates import DateFormatter
############################################
# generate data
x = np.arange(0,10)
y0 = np.arange(0,10)
y1 = np.random.rand(10)
y2 = np.random.rand(10)
############################################
# initialize (sub)plot(s)
fig, ax = plt.subplots()
############################################
# width of x axis
x_min_index = 0
x_max_index = 3
x_min = x[x_min_index]
x_max = x[x_max_index]
############################################
# create (sub)plot
l, = plt.plot(x,y0, label="plot1a")
l, = plt.plot(x,y1, label="plot1b")
l, = plt.plot(x,y2, label="plot1c")
# (sub)plot - set values
plt.title("plot1")
plt.xlabel('xlabel')
plt.ylabel('ylabel')
plt.legend()
plt.grid()
############################################
# apply for all (sub)plot(s)
plt.subplots_adjust(bottom=0.25)
############################################
# (sub)plot - get values
# plt.axis(x_min, x_max, y_min, y_max)
y_min = plt.axis()[2]
y_max = plt.axis()[3]
print y_min
print y_max
############################################
# (sub)plot - set values
# change only x values
# y values are set to same value
# plt.axis([x_min, x_max, y_min, y_max])
# (sub)plot - set values
# change only x values
plt.xlim(x_min, x_max)
############################################
# (sub)plot - get values
locs, labels = plt.xticks()
print locs
print labels
############################################
# (sub)plot - set values
plt.setp(labels, rotation=45)
plt.show()
此代码返回此图:
我希望能够控制所有这些属性,但创建多个子图,我的尝试是在这里:
#!/usr/bin/python
import datetime
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib.dates import DateFormatter
############################################
# generate data
x = np.arange(0,10)
y0 = np.arange(0,10)
y1 = np.random.rand(10)
y2 = np.random.rand(10)
############################################
# initialize (sub)plot(s)
fig, axarr = plt.subplots(2)
# https://stackoverflow.com/questions/6541123/improve-subplot-size-spacing-with-many-subplots-in-matplotlib
fig.tight_layout()
############################################
# width of x axis
x_min_index = 0
x_max_index = 3
x_min = x[x_min_index]
x_max = x[x_max_index]
############################################
# 1st (sub)plot
line0 = axarr[0].plot(x,y1, label="plot1a")
line0 = axarr[0].plot(x,y2, label="plot1b")
line0 = axarr[0].legend()
line0 = axarr[0].set_title('plot1')
line0 = axarr[0].set_xlabel('xlabel1')
line0 = axarr[0].set_ylabel('ylabel1')
line0 = axarr[0].grid()
############################################
# 2st (sub)plot
line1 = axarr[1].plot(x,y0, label="plot2", color="red")
line1 = axarr[1].legend()
line1 = axarr[1].set_title('plot2')
line1 = axarr[1].set_xlabel('xlabel2')
line1 = axarr[1].set_ylabel('ylabel2')
line1 = axarr[1].grid()
############################################
# apply for all (sub)plot(s)
plt.subplots_adjust(bottom=0.25)
############################################
# OLD CODE
# (sub)plot - get values
# plt.axis(x_min, x_max, y_min, y_max)
# y_min = plt.axis()[2]
# y_max = plt.axis()[3]
# print y_min
# print y_max
# NEW ALTERNATIVE
l0_x_min, l0_x_max = axarr[0].get_xlim()
l0_y_min, l0_y_max = axarr[0].get_ylim()
l1_x_min, l1_x_max = axarr[1].get_xlim()
l1_y_min, l1_y_max = axarr[1].get_ylim()
print l0_x_min
print l0_x_max
print l0_y_min
print l0_y_max
print l1_x_min
print l1_x_max
print l1_y_min
print l1_y_max
############################################
# OLD CODE
# (sub)plot - set values
# change only x values
# y values are set to same value
# plt.axis([x_min, x_max, y_min, y_max])
# (sub)plot - set values
# change only x values
# plt.xlim(x_min, x_max)
# NEW ALTERNATIVE
axarr[0].set_xlim(x_min, x_max)
############################################
# OLD CODE
# (sub)plot - get values
# locs, labels = plt.xticks()
# print locs
# print labels
# NEW ALTERNATIVE
line0_xticks = axarr[0].get_xticks()
line0_labels = axarr[0].get_xticklabels()
print line0_xticks
print line0_labels
line1_xticks = axarr[1].get_xticks()
line1_labels = axarr[1].get_xticklabels()
print line1_xticks
print line1_labels
############################################
# OLD CODE
# (sub)plot - set values
# plt.setp(labels, rotation=45)
# NEW ALTERNATIVE
############################################
plt.show()
此代码返回此图:
此代码有一个限制,因为我无法在x轴上设置标签的旋转.我找不到适当的子图的方法来做到这一点.到目前为止,我已经找到了以下相互对应的方法:
PLOT SUBPLOTS
plt.axis() axarr[0].get_xlim(), axarr[0].get_ylim()
plt.axis([x_min, x_max, y_min, y_max]) axarr[0].set_xlim(x_min, x_max), axarr[0].set_ylim(y_min, y_max)
plt.xlim(x_min, x_max) axarr[0].set_xlim(x_min, x_max)
plt.xticks() axarr[0].get_xticks(), axarr[0].get_xticklabels()
plt.setp(labels, rotation=45) ???
plt.title("plot1") axarr[0].set_title('plot1')
plt.grid() axarr[0].grid()
plt.legend() axarr[0].legend()
plt.xlabel('xlabel') axarr[0].set_xlabel('xlabel')
plt.ylabel('xlabel') axarr[0].set_ylabel('xlabel')
plt.plot(x,y, label="plot") axarr[0].plot(x,y, label="plot")
问题:
>是否存在类似的表,但是对于绘图和子图之间的所有相应方法?
>可能更多的哲学问题;为什么两种不同的命名惯例?
>我通过运行ipdb下的代码,点击选项卡,并记下熟悉的方法名称,将此表创建为试错.有没有更好的办法?我可以想象一些可以比较方法返回类型,参数等的东西.
>如何从另一个方法中找到任何方法的替代方法?
>最后,如何在子图下旋转x标签?
解决方法:
我将尝试通过1回答您的问题.但之前,您需要了解情节和子情节之间没有区别.虽然图通常是指图中的单个轴,但子图是指图中多个轴中的一个.但是对matplotlib来说,它们都是轴实例.
plt通常是指pyplot模块(如果导入为matt的导入matplotlib.pyplot).该模块具有许多功能,例如您列出的功能以及许多绘图功能(例如,绘图,散射等).但是,这些只是为了方便起见,并在当前轴实例上调用相应的函数.以非常简单的方式,plt.plot构建如下:
def plot(*args, **kwargs):
ax = plt.gca()
return ax.plot(*args, **kwargs)
不幸的是,命名不一致.但通常,axis方法使用set_ *和get_ *,而plt函数则不使用.
这意味着你可以使用fig,ax = plt.subplots(1)创建你发布的第一个图,然后继续使用axes方法:ax.plot(…)等.
至于你的问题:
>我不这么认为.但作为一般建议,我会尝试使用
轴方法,因为它们提供所有功能,而plt
等价物是有限的.
> matplotlib的灵感来自Matlab的绘图功能.这就是plt功能的来源.
>我不确定是否有一种肾上腺法.通常可以通过名称猜测事物(例如plt.title – > ax.set_title).使用matplotlib通常也意味着手头有文档.
>再次,根据经验或名称.但你没必要.你也可以使用一个!
> plt.setp是一个辅助函数.它为艺术家列表设置属性(因此得名).因此plt.setp(标签,旋转= 45)就像这样:
for i in labels:
i.set_rotation(45)
问题是如何获得对标签列表的引用.你可以通过以下方式获得它们:
labels = plt.xticklabels()
要么
labels = ax.get_xticklabels()
内容总结
以上是互联网集市为您收集整理的python – 用于子图的plt.setp替代或如何在x轴上为子图设置文本旋转全部内容,希望文章能够帮你解决python – 用于子图的plt.setp替代或如何在x轴上为子图设置文本旋转所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。