简体   繁体   English

如果条件为真,则行 plot 带有不同的标记 python 3

[英]Line plot with different markers if condition is true python 3

Iam trying to create a line plot from column 1 of an array.我正在尝试从数组的第 1 列创建一行 plot 。 The marker of the line plot should change if a certain condition in column 2 of the same array is full filled (marker='o' if condition is full filled, marker='x' if the condition is false. However, the result of my plot is incorrect.如果同一数组的第 2 列中的某个条件已满,则 plot 行的标记应更改(如果条件已满,则标记 ='o',如果条件为假,则标记 ='x'。但是,结果我的 plot 不正确。

import numpy as np
import matplotlib.pyplot as plt
import random

###These are 100 random numbers
randomlist = random.sample(range(0, 100), 100)

###This is an array with 50 rows and 2 columns
arr = np.array(randomlist)
arr_re = arr.reshape(50,2)

### This is a lineplot of column 1 with different markers dependent on the value of column 2
figure, ax = plt.subplots(figsize=(13, 6))
for i in range(0,50,1):
 #figure, ax = plt.subplots(figsize=(13, 6))
 if arr_re[i,1] > 50:
  ax.plot(arr_re[i,0], color="black", marker='o', label='1880-1999')
 else:
  ax.plot(arr_re[i,0], color="black", marker='x', label='1880-1999')
plt.show()

Maybe someone could give me a hint.也许有人可以给我一个提示。 Cheers icorrect_result plot should look like this, however with changing markers according to the condition of column2干杯icorrect_result plot 应该看起来像这样,但是根据 column2 的条件更改标记

The main problem of your code above is that you forgot to add an x-value to the plot function.上面代码的主要问题是您忘记向 plot function 添加 x 值。 A way to achieve what you aim is to first plot the line of random points and then plot a scatter of the points with varying marker.实现您的目标的一种方法是首先 plot 随机点线,然后 plot 使用不同标记的点的分散。 See my adjustments to your code below.请参阅下面我对您的代码的调整。

import numpy as np
import matplotlib.pyplot as plt
import random

###These are 100 random numbers
randomlist = random.sample(range(0, 100), 100)

###This is an array with 50 rows and 2 columns
arr = np.array(randomlist)
arr_re = arr.reshape(50,2)

### This is a lineplot of column 1 with different markers dependent on the value of column 2
figure, ax = plt.subplots(figsize=(13, 6))

# plot column 1
plt.plot(arr_re[:,0])

# scatter plot the markers based on a condition
for i in range(0,50,1):
    if arr_re[i,1] > 50:
        ax.scatter(i,arr_re[i,0], color="black", marker='o', label='1880-1999')
    else:
        ax.scatter(i,arr_re[i,0], color="black", marker='x', label='1880-1999')
plt.show()

The result is:结果是:

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM