How to change plotline color in python? from blue to black?

I get stuck when I generated a dataset and tried to colorize the plot line in python. For example, I would like to change the line color from blue to black here. This is what I have and returns - this is the dataset that I received in pandas form. Thank!

ax=plt.gca()
ax.set_axis_bgcolor('#cccccc')
returns.plot()

enter image description here

+4
source share
1 answer

The usual way to set the line color in matplotlib is to specify it in the plot command. This can be done as a string after the data, for example. "r-"for a red line or explicitly specifying an argument color.

import matplotlib.pyplot as plt

plt.plot([1,2,3], [2,3,1], "r-") # red line
plt.plot([1,2,3], [5,5,3], color="blue") # blue line

plt.show()

See also the documentation.

, lines2D.set_color().

line, = plt.plot([1,2,3], [4,5,3], color="blue")
line.set_color("black")


pandas :
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({ "x" : [1,2,3,5], "y" : [3,5,2,6]})
df.plot("x", "y", color="r") #plot red line

plt.show()

,

plt.gca().get_lines()[0].set_color("black")

( ) .
,

for ax in plt.gcf().axes:
    ax.get_lines()[0].set_color("black")

, .

+11

Source: https://habr.com/ru/post/1667082/


All Articles