当前位置:网站首页>Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
2022-06-11 06:56:00 【煮酒cos】
基础绘图案例
# coding: utf-8
import matplotlib.pyplot as plt
# figsize = 11, 9
# figure, ax = plt.subplots(figsize = figsize)
x1 =[0,5000,10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 55000]
y1=[0, 223, 488, 673, 870, 1027, 1193, 1407, 1609, 1791, 2113, 2388]
x2 = [0, 5000, 10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 55000]
y2 = [0, 214, 445, 627, 800, 956, 1090, 1281, 1489, 1625, 1896, 2151]
# 设置输出的图片大小
figsize = 11, 9
figure, ax = plt.subplots(figsize=figsize)
# 在同一幅图片上画两条折线
A, = plt.plot(x1, y1, '-r', label='A', linewidth=5.0)
B, = plt.plot(x2, y2, 'b-.', label='B', linewidth=5.0)
# 设置图例并且设置图例的字体及大小
font1 = {
'family': 'Times New Roman',
'weight': 'normal',
'size': 23,
}
legend = plt.legend(handles=[A, B], prop=font1)
# 设置坐标刻度值的大小以及刻度值的字体
plt.tick_params(labelsize=23)
labels = ax.get_xticklabels() + ax.get_yticklabels()
# print labels
[label.set_fontname('Times New Roman') for label in labels]
# 设置横纵坐标的名称以及对应字体格式
font2 = {
'family': 'Times New Roman',
'weight': 'normal',
'size': 30,
}
plt.xlabel('round', font2)
plt.ylabel('value', font2)
plt.show()
python-增加matplotlib中图例行的线宽
我知道,如果更改线的线宽,则会在图例中自动更新。但是,我只想更改图例的线宽而不影响绘图。
现在有如下四种解决方案
获取leg对象
#这是一个简单的例子:
import numpy as np
import matplotlib.pyplot as plt
# make some data
x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
# plot sin(x) and cos(x)
p1 = plt.plot(x, y1, 'b-', linewidth=1.0)
p2 = plt.plot(x, y2, 'r-', linewidth=1.0)
# make a legend for both plots
leg = plt.legend([p1, p2], ['sin(x)', 'cos(x)'], loc=1)
# set the linewidth of each legend object
for legobj in leg.legendHandles:
legobj.set_linewidth(2.0)
plt.show()
pyplot提供的api
@Brendan Wood的方法使用pyplot提供的api。在matplotlib中,首选使用轴的面向对象样式。 下面是如何使用axes方法实现此目的的方法。
import numpy as np
import matplotlib.pyplot as plt
# make some data
x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
fig, ax = plt.subplots()
ax.plot(x, y1, linewidth=1.0, label='sin(x)'
ax.plot(x, y2, linewidth=1.0, label='cos(x)')
leg = ax.legend()
for line in leg.get_lines():
line.set_linewidth(4.0)
plt.show()
自定义方案
如果要更改绘图中的所有线条,定义自己的图例处理程序可能会很有用:
import matplotlib.pyplot as plt
from matplotlib import legend_handler
from matplotlib.lines import Line2D
import numpy as np
class MyHandlerLine2D(legend_handler.HandlerLine2D):
def create_artists(self, legend, orig_handle,xdescent, ydescent, width, height, fontsize,trans):
xdata, xdata_marker = self.get_xdata(legend, xdescent, ydescent,width, height, fontsize)
ydata = ((height-ydescent)/2.)*np.ones(xdata.shape, float)
legline = Line2D(xdata, ydata)
self.update_prop(legline, orig_handle, legend)
#legline.update_from(orig_handle)
#legend._set_artist_props(legline) # after update
#legline.set_clip_box(None)
#legline.set_clip_path(None)
legline.set_drawstyle('default')
legline.set_marker("")
legline.set_linewidth(10)
legline_marker = Line2D(xdata_marker, ydata[:len(xdata_marker)])
self.update_prop(legline_marker, orig_handle, legend)
#legline_marker.update_from(orig_handle)
#legend._set_artist_props(legline_marker)
#legline_marker.set_clip_box(None)
#legline_marker.set_clip_path(None)
legline_marker.set_linestyle('None')
if legend.markerscale != 1:
newsz = legline_marker.get_markersize()*legend.markerscale
legline_marker.set_markersize(newsz)
# we don't want to add this to the return list because
# the texts and handles are assumed to be in one-to-one
# correpondence.
legline._legmarker = legline_marker
return [legline, legline_marker]
plt.plot( [0, 1], [0, 1], '-r', lw=1, label='Line' )
plt.legend(handler_map={
Line2D:MyHandlerLine2D()})
plt.show()
使用画布上艺术家的副本
默认情况下,图例包含行本身。 因此,更改画布中线条的线宽也会更改图例中的线条(反之亦然,因为它们本质上是同一对象)。
一种可能的解决方案是使用画布上艺术家的副本,并仅更改副本的线宽。
import numpy as np
import matplotlib.pyplot as plt
import copy
x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, c='b', label='y1',linewidth=1.0)
ax.plot(x, y2, c='r', label='y2')
# obtain the handles and labels from the figure
handles, labels = ax.get_legend_handles_labels()
# copy the handles
handles = [copy.copy(ha) for ha in handles ]
# set the linewidths to the copies
[ha.set_linewidth(7) for ha in handles ]
# put the copies into the legend
leg = plt.legend(handles=handles, labels=labels)
plt.savefig('leg_example')
plt.show()
使用handler_map和更新功能
另一种选择是使用handler_map和更新功能。 这以某种方式是自动的,指定处理程序映射将自动使图例中的任何行宽7点。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerLine2D
x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, c='b', label='y1',linewidth=1.0)
ax.plot(x, y2, c='r', label='y2')
linewidth=7
def update(handle, orig):
handle.update_from(orig)
handle.set_linewidth(7)
plt.legend(handler_map={
plt.Line2D : HandlerLine2D(update_func=update)})
plt.show()
结果与上面相同。
边栏推荐
- Throttling and anti shake
- 【概率论与数理统计】猴博士 笔记 p41-44 统计量相关小题、三大分布的判定、性质、总体服从正态分布的统计量小题
- Difference between byte and bit
- 迅为干货 |瑞芯微RK3568开发板TFTP&NFS烧写(上)
- Illustration of JS implementation from insertion sort to binary insertion sort [with source code]
- Notice on organizing the application for the first edition of Ningbo key software in 2022
- Henan college entrance examination vs Tianjin college entrance examination (2008-2021)
- Simple integration of client go gin six list watch two (about the improvement of RS, pod and deployment)
- Lazy load
- JS judge whether the year is a leap year and the number of days in the month
猜你喜欢

Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。

争议很大的问题

Do you know what the quotation for it talent assignment service is? It is recommended that programmers also understand

WPF 数据绑定(四)

Latex various arrows / arrows with text labels / variable length arrows

Whether the ZABBIX monitoring host is online

About the designer of qtcreator the solution to the problem that qtdesigner can't pull and hold controls normally

During unity panoramic roaming, AWSD is used to control lens movement, EQ is used to control lens lifting, and the right mouse button is used to control lens rotation.

Analysis of key points and difficulties of ES6 promise source code

Drawing with qpainter
随机推荐
网狐游戏服务器房间配置约战定制功能实现
[MATLAB image fusion] particle swarm optimization adaptive multispectral image fusion [including source code phase 004]
. Net C Foundation (6): namespace - scope with name
Detailed explanation of mutual call between C language and Lua
Start the Nacos server of shell script
开源漫画服务器Mango
你知道IT人才外派服务报价是怎样的么?建议程序员也了解下
VTK vtkplane and vtkcutter use
Resolve typeerror: ctx injections. tableRoot.$ scopedSlots[ctx.props.column.slot] is not a function
【概率论与数理统计】猴博士 笔记 p41-44 统计量相关小题、三大分布的判定、性质、总体服从正态分布的统计量小题
Common modules of saltstack
Promises/a+ standard Chinese Translation
Pytest automated test - easy tutorial (01)
Text overflow failure
The nearest common ancestor of 235 binary search tree
Duality-Gated Mutual Condition Network for RGBT Tracking
Leetcode hot topic 100 topic 6-10 solution
Shutter restraint container assembly
Method to determine whether it is an array
538. convert binary search tree to cumulative tree