简体   繁体   English

如何绘制带有x和y分类轴的线图?

[英]How to plot line graph with x and y categorical axis?

I'm trying to plot a line graph that compares 2 categorical variables. 我正在尝试绘制比较2个类别变量的折线图。 However I keep running into errors. 但是我一直遇到错误。 I've put my code below: 我将代码放在下面:

import matplotlib.pyplot as plt

cat = ["bored", "happy", "bored", "bored", "happy", "bored"]
dog = ["happy", "happy", "happy", "happy", "bored", "bored"]
activity = ["combing", "drinking", "feeding", "napping", "playing", 
"washing"]

fig, ax = plt.subplots()
ax.plot(activity, dog, label="dog")
ax.plot(activity, cat, label="cat")
ax.legend()

plt.show()

To provide an answer here: When being run with matplotlib >=2.1, the code from the question 在此处提供答案:当使用matplotlib> = 2.1运行时,来自问题的代码

import matplotlib.pyplot as plt

cat = ["bored", "happy", "bored", "bored", "happy", "bored"]
dog = ["happy", "happy", "happy", "happy", "bored", "bored"]
activity = ["combing", "drinking", "feeding", "napping", "playing", 
"washing"]

fig, ax = plt.subplots()
ax.plot(activity, dog, label="dog")
ax.plot(activity, cat, label="cat")
ax.legend()

plt.show()

runs fine and produces 运行良好并产生

在此处输入图片说明


For earlier versions of matplotlib, axes should be numeric. 对于较早版本的matplotlib,轴应为数字。 Hence you need to convert the categories into numbers, plot them, then set the ticklabels accordingly. 因此,您需要将类别转换为数字,绘制它们,然后相应地设置刻度标签。

 import numpy as np import matplotlib.pyplot as plt cat = ["bored", "happy", "bored", "bored", "happy", "bored"] dog = ["happy", "happy", "happy", "happy", "bored", "bored"] activity = ["combing", "drinking", "feeding", "napping", "playing", "washing"] catu, cati = np.unique(cat, return_inverse=True) dogu, dogi = np.unique(dog, return_inverse=True) fig, ax = plt.subplots() ax.plot(range(len(dog)), dogi, label="dog") ax.plot(range(len(cat)), cati, label="cat") ax.set_xticks(range(len(activity))) ax.set_xticklabels(activity) ax.set_yticks(range(len(catu))) ax.set_yticklabels(catu) ax.legend() plt.show() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何 plot 在 x 轴上有 2 个变量并在 y 轴上计数的图形? - How to plot a graph with 2 variables on the x axis and count on the y axis? Matplotlib:如何在x轴上绘制带有分类数据的线? - Matplotlib: how to plot a line with categorical data on the x-axis? 如何在Y轴上聚合数据并在Python中绘制折线图 - how to aggregate data in Y axis and plot a line graph in python 如何在 seaborn scatter plot 中添加 x 和 y 轴线 - How to add x and y axis line in seaborn scatter plot 如何从绘图图中消失 X 和 Y 轴线 - How to vanish X and Y axis line from plotly graph Matplotlib:如何在y轴上绘制分类数据? - Matplotlib: how to plot categorical data on the y-axis? 如何用pyplot在相同的x轴(日期时间)但不同的y轴上绘制线形图和条形图? - How to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot? python pandas:如何为折线图切换x轴和y轴 - python pandas: how to switch x-axis with y-axis for a line graph 分类y轴和日期时间x轴与Bokeh vbar图 - categorical y-axis and datetime x-axis with Bokeh vbar plot 在Python中为分类变量绘制数字Y轴,X轴时间序列的最佳方法是什么? - What is the best way to plot numerical Y axis, X axis Time series for a categorical variable in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM