24小时热门版块排行榜    

查看: 3142  |  回复: 8

seu-ljc

木虫 (正式写手)

[求助] python/matplotlib 图例如何放在外面?已有1人参与

from pylab import *
fig = figure()
x=linspace(0,1,10)
y1=x
y2=x**2
plot(x,y1,'k',linewidth=2,label="y1"
plot(x,y2,'r',linewidth=2,label="y2"
leg=legend(numpoints=1,fontsize=18)
legend(loc='center left', bbox_to_anchor=(1, 0.5))
leg.get_frame().set_alpha(0.0)
grid()
show()
fig.savefig('o.png', transparent=True)

我用了这个命令
legend(loc='center left', bbox_to_anchor=(1, 0.5))

结果变成这样了
python/matplotlib  图例如何放在外面?


请高手支招~多谢~
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

aprillf

木虫 (正式写手)

【答案】应助回帖

感谢参与,应助指数 +1
看matplotlib的帮助文档即可
http://matplotlib.org/1.3.1/users/legend_guide.html
CODE:
from matplotlib.pyplot import *

subplot(211)
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")
legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
       ncol=2, mode="expand", borderaxespad=0.)

subplot(223)
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")
legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)


show()

2楼2015-04-12 14:55:58
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

seu-ljc

木虫 (正式写手)

引用回帖:
2楼: Originally posted by aprillf at 2015-04-12 14:55:58
看matplotlib的帮助文档即可
http://matplotlib.org/1.3.1/users/legend_guide.html


from matplotlib.pyplot import *

subplot(211)
plot(, label="test1"
plot(, label="test2"
...

这个我看过,现在目前是我要一个图即可,而不是两个图~按这个做出来的效果如下
python/matplotlib  图例如何放在外面?-1
3楼2015-04-12 16:05:34
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

seu-ljc

木虫 (正式写手)

引用回帖:
2楼: Originally posted by aprillf at 2015-04-12 14:55:58
看matplotlib的帮助文档即可
http://matplotlib.org/1.3.1/users/legend_guide.html


from matplotlib.pyplot import *

subplot(211)
plot(, label="test1"
plot(, label="test2"
...

有大块的空白地。。。
python/matplotlib  图例如何放在外面?-2
4楼2015-04-12 16:07:25
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

aprillf

木虫 (正式写手)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
seu-ljc: 金币+24, ★★★很有帮助, O(∩_∩)O谢谢 2015-04-14 13:12:30
引用回帖:
4楼: Originally posted by seu-ljc at 2015-04-12 16:07:25
有大块的空白地。。。
...

一个图就把subplot()语句去掉
5楼2015-04-13 08:46:22
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

seu-ljc

木虫 (正式写手)

引用回帖:
5楼: Originally posted by aprillf at 2015-04-13 08:46:22
一个图就把subplot()语句去掉...


    import matplotlib.pyplot as plt
    from pylab import *
    # data
    x=linspace(0,1,10)
    y1=x
    y2=x**2

    # Plot
    fig = plt.figure(1)
    ax = fig.add_subplot(111)
    ax.plot(x, y1,label="y1"
    ax.plot(x, y2,label="y1"
    # Add legend, title and axis labels

    # lgd.get_frame().set_alpha(0.0)


    ax.set_xlabel('x label')
    ax.set_ylabel('y label')   
    legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)


    show()
    fig.savefig('image_output.png',  transparent=True)
  • null


去掉了就显示不了图例了啊
python/matplotlib  图例如何放在外面?-3
6楼2015-04-13 14:35:24
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

cooooldog

铁杆木虫 (著名写手)

ส็็็

引用回帖:
6楼: Originally posted by seu-ljc at 2015-04-13 14:35:24
import matplotlib.pyplot as plt
from pylab import *
# data
x=linspace(0,1,10)
y1=x
y2=x**2

# Plot
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot(x, y1,label="y1"
ax. ...

如果这个可以,分数给我偶像aprillf吧
CODE:
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

for i in xrange(5):
    ax.plot(x, i * x, label='$y = %ix$'%i)

# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

plt.show()

ส็็็็็็็็็็็็็็็็็็็็
7楼2015-04-14 11:43:17
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

cooooldog

铁杆木虫 (著名写手)

ส็็็

引用回帖:
7楼: Originally posted by cooooldog at 2015-04-14 11:43:17
如果这个可以,分数给我偶像aprillf吧

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

for i in xrange(5):
    ax.plot(x, i * ...

CODE:
import matplotlib.pyplot as plt
import numpy as np

x=np.arange(10)
y=x**2
fig= plt.figure()
ax =plt.subplot(111)

ax.plot(x,x,label='$y=x$')
ax.plot(x,y,label='$y=x^2$')

box=ax.get_position()
ax.set_position([box.x0,box.y0,box.width*0.8,box.height])
ax.legend(loc='upper left',bbox_to_anchor=(1.0,0.5))
plt.show()

输出
python/matplotlib  图例如何放在外面?-4
ส็็็็็็็็็็็็็็็็็็็็
8楼2015-04-14 12:07:51
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

seu-ljc

木虫 (正式写手)

引用回帖:
7楼: Originally posted by cooooldog at 2015-04-14 11:43:17
如果这个可以,分数给我偶像aprillf吧

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

for i in xrange(5):
    ax.plot(x, i * ...

遵命,金币按你的要求已给aprillf大人~
9楼2015-04-14 13:11:59
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 seu-ljc 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[教师之家] 请问事业编制和年薪制冲突吗? +7 ZHONGWU_U 2024-06-14 7/350 2024-06-16 14:27 by 听风Ming
[找工作] 初始合伙人来啦!(生物试剂耗材标准品) +7 欢快的小科研人 2024-06-15 12/600 2024-06-16 14:11 by 水水水中游
[论文投稿] 二审返修送审10天了,原来一审的3个审稿人只有2个接受了审稿,会邀请新审稿人么? 50+3 huanpo116 2024-06-15 5/250 2024-06-16 10:27 by bobvan
[找工作] 成都产品质量检测研究院 200+3 鲸鱼663 2024-06-11 9/450 2024-06-16 10:08 by SNaiL1995
[论文投稿] 投稿被一个审稿人恶意评审了怎么样? +5 1chen 2024-06-14 7/350 2024-06-15 23:15 by xy66xy
[教师之家] 我们学院常年位居 各学院 倒数第二。专业撤销的话,在编者有什么补偿? +13 河西夜郎 2024-06-09 14/700 2024-06-15 19:44 by LittleBush
[基金申请] BO4的YQ答辩通知发布了吗? +6 博学笃行 2024-06-11 6/300 2024-06-15 16:04 by 悲催科研狗
[基金申请] 2024国社科通讯评审 +9 qsd10086 2024-06-13 14/700 2024-06-15 15:51 by thesuna
[基金申请] E12面上申请 +4 汉风之遗 2024-06-13 4/200 2024-06-14 15:28 by 天外飞去来
[硕博家园] 关于硕博连读的一些疑问? +4 Lwenter 2024-06-14 4/200 2024-06-14 14:32 by ou0551
[有机交流] ππ堆积会发生在有机溶剂中吗 5+3 zibuyu0420 2024-06-13 4/200 2024-06-14 14:17 by 小肉干
[基金申请] 75批博后基金 +10 kyukitu 2024-06-13 13/650 2024-06-14 10:31 by kyukitu
[论文投稿] 文章proof要求使用机构的邮箱 5+3 不可不信缘 2024-06-11 11/550 2024-06-14 07:00 by 3001160025
[基金申请] 连续两年医学口青年项目初审体会 +11 进击的荣耀 2024-06-09 18/900 2024-06-13 17:27 by 进击的荣耀
[硕博家园] 机械研究生如何拿到年薪40+w +13 阿巴阿巴哦哦 2024-06-11 15/750 2024-06-13 15:40 by 113745685
[考博] 博导选择 +3 bing85977 2024-06-12 3/150 2024-06-13 15:34 by 我是邱尧
[论文投稿] 摩擦磨损论文投稿 +3 jmysan 2024-06-12 3/150 2024-06-13 08:36 by 莱茵润色
[硕博家园] 申博 +3 悦悦小小鱼 2024-06-12 3/150 2024-06-12 15:11 by chen5805
[硕博家园] 求助 +6 LYWwrz 2024-06-09 9/450 2024-06-11 13:12 by powerhours
[论文投稿] water research状态咨询 5+3 Flyyawa 2024-06-10 6/300 2024-06-11 09:45 by bobvan
信息提示
请填处理意见