python - 以字符串为 x 轴绘制数据

标签 python matplotlib scatter-plot

我在 pandas 数据框中有以下数据集,但在 x 轴上绘制 timeOfDay 和 y 轴上绘制 Wattage 时遇到问题。基本上,我试图制作一个散点图(plt.scatter),其中x轴有3点(早上、下午、晚上),以及它们各自的Wattages 高于 timeOfDay

因此,Morning 的三个数据点将位于其上方,Afternoon 的数据点将位于其上方,依此类推。

  Wattage        time_stamp       timeOfDay
0    100      2015-02-24 10:00:00    Morning
1    120      2015-02-24 11:00:00    Morning
2    104      2015-02-24 12:00:00    Morning
3    105      2015-02-24 13:00:00  Afternoon
4    109      2015-02-24 14:00:00  Afternoon
5    120      2015-02-24 15:00:00  Afternoon
6    450      2015-02-24 16:00:00  Afternoon
7    200      2015-02-24 17:00:00    Evening
8    300      2015-02-24 18:00:00    Evening
9    190      2015-02-24 19:00:00    Evening
10   100      2015-02-24 20:00:00    Evening
11   110      2015-02-24 21:00:00    Evening

最佳答案

您可以使用xticks方法:

import numpy as np
import matplotlib.pyplot as plt

y = np.array( [100, 120, 104, 105, 109, 120, 450, 200, 300, 190, 100, 110] )

x = []   

labels = [ 'Morning', 'Morning','Morning','Afternoon','Afternoon','Afternoon','Afternoon', 'Evening', 'Evening', 'Evening', 'Evening', 'Evening']

for q in labels:
    if q == 'Morning':
        x.append(0)
    elif q == 'Afternoon':
        x.append(1)
    elif q == 'Evening':
        x.append(2)

plt.scatter(x, y)
plt.xticks( x, labels )
plt.show()

关于python - 以字符串为 x 轴绘制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42004465/

相关文章:

javascript - 在 Highcharts 中显示高于零的负 Y 值而不是低于零

python - 如何将 3D 散点投影到 xy 平面上?

Python Tkinter : Why use Tkinter. W 不是 str "w"

python - 如何对具有尺寸的单位进行对数?

python - 如何删除 matplotlib 的通用内容,无论绘制的类型如何

python - 在python中堆叠来自不同系列的三个条形图

python - Matplotlib 散点图过滤器颜色(Colorbar)

python - 如何并行运行生成器代码?

python - 如何从 Apache Airflow 使用 DockerOperator

python - 第一个元素未绘制在散点图上