我有一个带有两个y轴的情节,使用twinx().我也给线条贴了标签,并希望用它们来展示legend(),但我只是成功地在图例中获得了一个轴的标签:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(time, Swdown, '-', label = 'Swdown')
ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
ax2.plot(time, temp, '-r', label = 'temp')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)")
ax2.set_ylabel(r"Temperature ($^\circ$C)")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)
plt.show()
Run Code Online (Sandbox Code Playgroud)
所以我只得到图例中第一个轴的标签,而不是第二个轴的标签"temp".我怎么能将这第三个标签添加到图例中?

要在matplotlib图中添加图例,只需运行即可legend().
如何从情节中删除图例?
(我最接近的是运行legend([])以便从数据中清空图例.但是在右上角留下了一个丑陋的白色矩形.)
我有一个使用pandas Dataframe中不同列创建的多个条形图.
fig1 = plt.figure()
ypos = np.arange(len(dframe))
colorscheme = seaborn.color_palette(n_colors=4)
accuracyFig = fig1.add_subplot(221)
accuracyFig.bar(ypos,dframe['accuracy'], align = 'center', color=colorscheme)
accuracyFig.set_xticks([0,1,2,3])
accuracyFig.set_ylim([0.5,1])
sensitivityFig = fig1.add_subplot(222)
sensitivityFig.bar(ypos, dframe['sensitivity'], align = 'center',color=colorscheme )
sensitivityFig.set_xticks([0,1,2,3])
sensitivityFig.set_ylim([0.5,1])
specificityFig = fig1.add_subplot(223)
specificityFig.bar(ypos, dframe['specificity'], align = 'center', color=colorscheme)
specificityFig.set_xticks([0,1,2,3])
specificityFig.set_ylim([0.5,1])
precisionFig = fig1.add_subplot(224)
precisionFig.bar(ypos, dframe['precision'], align = 'center', color=colorscheme)
precisionFig.set_xticks([0,1,2,3])
precisionFig.set_ylim([0.5,1])
Run Code Online (Sandbox Code Playgroud)
哪里dframe是带有整数值的pandas数据帧.这给我输出了下图
.
每种颜色对应于一个分类器模型 - perceptron,C2,C3 and C4存储在熊猫中dframe['name']
现在我想为整个人物绘制一个单一的图例.我尝试了以下内容
leg = plt.legend(dframe['name'])
Run Code Online (Sandbox Code Playgroud)
有关如何绘制单个图例并将其放在2个列中的图形的任何帮助.
这是我的数据框架
name accuracy sensitivity specificity precision
0 perceptron …Run Code Online (Sandbox Code Playgroud) 我想在pyqt GUI中为matplotlib(python)中的多个图创建一个自定义图例.(pyqt建议不要使用pyplot,因此必须使用面向对象的方法).
多个绘图将出现在网格中,但用户可以定义要显示的绘图数量.我希望图例出现在所有图的右侧,因此我不能简单地为最后的轴绘制图例.我希望为整个图形创建图例而不仅仅是最后一个轴(类似于pyplot中的plt.figlegend).
在其他地方看到的例子中,这需要参考绘制的线条.同样,我无法做到这一点,因为用户可以选择在图表上显示哪些线条,我宁愿总是显示所有可能的线条,无论它们当前是否显示.
(注意下面的示例代码使用pyplot但我的最终版本不能)
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import numpy as np
fig = plt.figure()
# Create plots in 2x2 grid
for plot in range(4):
# Create plots
x = np.arange(0, 10, 0.1)
y = np.random.randn(len(x))
y2 = np.random.randn(len(x))
ax = fig.add_subplot(2,2,plot+1)
plt.plot(x, y, label="y")
plt.plot(x, y2, label="y2")
# Create custom legend
blue_line = mlines.Line2D([], [], color='blue',markersize=15, label='Blue line')
green_line = mlines.Line2D([], [], color='green', markersize=15, label='Green line')
ax.legend(handles=[blue_line,green_line],bbox_to_anchor=(1.05, 0), loc='lower left', …Run Code Online (Sandbox Code Playgroud) 我正在使用“iris.csv”数据制作箱线图。我试图通过测量(即花瓣长度、花瓣宽度、萼片长度、萼片宽度)将数据分解为多个数据框,然后在 forloop 上绘制箱线图,从而添加子图。
最后,我想立即为所有箱线图添加一个通用图例。但是,我做不到。我已经使用几个 stackoverflow 问题尝试了几个教程和方法,但我无法修复它。
这是我的代码:
import seaborn as sns
from matplotlib import pyplot
iris_data = "iris.csv"
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = read_csv(iris_data, names=names)
# Reindex the dataset by species so it can be pivoted for each species
reindexed_dataset = dataset.set_index(dataset.groupby('class').cumcount())
cols_to_pivot = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width']
# empty dataframe
reshaped_dataset = pd.DataFrame()
for var_name in cols_to_pivot:
pivoted_dataset = reindexed_dataset.pivot(columns='class', values=var_name).rename_axis(None,axis=1)
pivoted_dataset['measurement'] = var_name
reshaped_dataset = reshaped_dataset.append(pivoted_dataset, ignore_index=True)
## Now, lets spit the dataframe into …Run Code Online (Sandbox Code Playgroud)