python子图命名_python – 用于子图的plt.setp替代或如何在x轴...

我有这个代码,我可以控制以下属性: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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
for ax_row in axes: for ax in ax_row: ax.yaxis.grid(True) # 在每个上添加y轴网格线 # 在y轴上添加网格线 ax.set_xticks([y + 1 for y in range(len(data1))]) # 指定x轴的轴刻度个数 ## [y+1 for y in range(len(all_data))]运行结果是[1,2,3] ax.set_xlabel('Method') # 设置x轴名称 ax.set_ylabel('Error/MW') # 设置y轴名称 # 添加刻度 # 添加刻度名称,我们需要使用 plt.setp() 函数: # 加刻度名称 plt.setp(axes[0,0], xticks=[1, 2, 3], xticklabels=['No Clustering','catboost-FM-First Clustering','catboost-FM-Second Clustering']) plt.setp(axes[1,0], xticks=[1, 2, 3], xticklabels=['No Clustering','catboost-kM-First Clustering','catboost-kM-Second Clustering']) plt.setp(axes[2,0], xticks=[1, 2, 3], xticklabels=['No Clustering', 'catboost-kMD-First Clustering', 'catboost-kMD-Second Clustering']) plt.setp(axes[0,1], xticks=[1, 2, 3], xticklabels=['No Clustering','xgboost-FM-First Clustering','xgboost-FM-Second Clustering']) plt.setp(axes[1,1], xticks=[1, 2, 3], xticklabels=['No Clustering','xgboost-kM-First Clustering','xgboost-kM-Second Clustering']) plt.setp(axes[2,1], xticks=[1, 2, 3], xticklabels=['No Clustering', 'xgboost-kMD-First Clustering', 'xgboost-kMD-Second Clustering']) plt.setp(axes[0,2], xticks=[1, 2, 3], xticklabels=['No Clustering','lightgbm-FM-First Clustering','lightgbm-FM-Second Clustering']) plt.setp(axes[1,2], xticks=[1, 2, 3], xticklabels=['No Clustering','lightgbm-kM-First Clustering','lightgbm-kM-Second Clustering']) plt.setp(axes[2,2], xticks=[1, 2, 3], xticklabels=['No Clustering', 'lightgbm-kMD-First Clustering', 'lightgbm-kMD-Second Clustering']) # 我们的刻度数是哪些,以及我们想要它添加的刻度标签是什么。 plt.show()请在我的代码里设置x轴的标签对象倾斜显示
最新发布
07-14

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值