欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python画散点图

程序员文章站 2022-07-03 20:04:59
import matplotlib.pyplot as pltimport numpy as npimport pandas as pdmy_dpi=96plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })plt.plot( 'x', 'y', data=df, lines....
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
plt.plot( 'x', 'y', data=df, linestyle='none', marker='o')

plt.annotate(
        # 标签和协调
        'This point is interesting!', xy=(25, 50), xytext=(0, 80), 
        
        # 自定义箭头
        arrowprops=dict(facecolor='black', shrink=0.05) 
        )
plt.show()

Python画散点图

import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
my_dpi=96
fig1 = plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
ax1 = fig1.add_subplot(111)
ax1.plot( 'x', 'y', data=df, linestyle='none', marker='o')
ax1.add_patch(
    patches.Rectangle(
        (20, 25),   # (x,y)
        50,         # 宽度
        50,         # 高度
        # 可以使用“角度”添加旋转
        alpha=0.3, facecolor="red", edgecolor="black", linewidth=3,  linestyle='solid'
    )
)
plt.show()

 Python画散点图

import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
my_dpi=96
fig1 = plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
ax1 = fig1.add_subplot(111)
ax1.plot( 'x', 'y', data=df, linestyle='none', marker='o')
ax1.add_patch(
    patches.Circle(
        (40, 35),   # (x,y)
        30,         # 半径
        alpha=0.3, facecolor="green", edgecolor="black", linewidth=1,  linestyle='solid'
    )
)
plt.show()

 Python画散点图

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
plt.plot( 'x', 'y', data=df, linestyle='none', marker='o')
plt.plot([80, 40], [30, 90], color="skyblue", lw=5, linestyle='solid', label="_not in legend")

plt.show()

 Python画散点图

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
plt.plot( 'x', 'y', data=df, linestyle='none', marker='o')
plt.axvline(40, color='r')
plt.axhline(40, color='green')
plt.show()

 Python画散点图

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
plt.plot( 'x', 'y', data=df, linestyle='none', marker='o')
plt.text(40, 00, r'equation: $\sum_{i=0}^\infty x_i$', fontsize=20)
plt.show()

 Python画散点图

import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
my_dpi=96
fig1 = plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

ax1 = fig1.add_subplot(111, aspect='equal')
ax1.plot( 'x', 'y', data=df, linestyle='none', marker='o')
ax1.add_patch(
    patches.Ellipse(
        (40, 35),   # (x,y)
        30,         # 宽度
        100,        # 高度
        45,         # 半径
        alpha=0.3, facecolor="green", edgecolor="black", linewidth=1,  linestyle='solid'
    )
)
plt.show()

 Python画散点图

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

plt.subplot(121)
plt.plot( 'x', 'y', data=df, marker='o', alpha=0.4)
plt.title("A subplot with 2 lines")
plt.subplot(122)
plt.plot( 'x','z', data=df, linestyle='none', marker='o', color="orange", alpha=0.3)
plt.show()

 Python画散点图

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

plt.subplot(211)
plt.plot( 'x', 'y', data=df, marker='o', alpha=0.4)
plt.title("A subplot with 2 lines")
plt.subplot(212)
plt.plot( 'x','z', data=df, linestyle='none', marker='o', color="orange", alpha=0.3)

plt.show()

 Python画散点图

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*10 })
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=True)
axes[0].plot( 'x', 'y', data=df, marker='o', alpha=0.4)
axes[1].plot( 'x','z', data=df, linestyle='none', marker='o', color="orange", alpha=0.3)
axes[0].title.set_text('These 2 plots have the same limit for the Y axis')

plt.show()

 Python画散点图

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

plt.subplot(221)
plt.plot( 'x', 'y', data=df, marker='o', alpha=0.4)
plt.subplot(222)
plt.plot( 'x','z', data=df, linestyle='none', marker='o', color="orange", alpha=0.3)
plt.subplot(223)
plt.plot( 'x','z', data=df, linestyle='none', marker='D', color="green", alpha=0.3)
plt.subplot(224)
plt.plot( 'x','z', data=df, marker='o', color="grey", alpha=0.3)

plt.show()

 Python画散点图

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

fig=plt.figure()

plt.subplot(221)
plt.plot( 'x', 'y', data=df, marker='o', alpha=0.4)
plt.title('title of fig A', fontsize=10, color='grey', loc='left', style='italic')
plt.tick_params(labelbottom='off', bottom='off')
plt.subplot(222)
plt.plot( 'x','z', data=df, linestyle='none', marker='o', color="orange", alpha=0.3)
plt.title('title of fig B', fontsize=10, color='grey', loc='left', style='italic')
plt.tick_params(labelbottom='off', bottom='off')
plt.subplot(223)
plt.plot( 'x','z', data=df, linestyle='none', marker='D', color="green", alpha=0.3)
plt.title('title of fig C', fontsize=10, color='grey', loc='left', style='italic')
plt.subplot(224)
plt.plot( 'x','z', data=df, marker='o', color="grey", alpha=0.3)
plt.title('title of fig D', fontsize=10, color='grey', loc='left', style='italic')

plt.suptitle('A title common to my 4 plots', y=1.02)
plt.show()

 Python画散点图

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })
ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2)
ax1.plot( 'x', 'y', data=df, marker='o', alpha=0.4)
ax2 = plt.subplot2grid((2, 2), (1, 0), colspan=1)
ax2.plot( 'x','z', data=df, marker='o', color="grey", alpha=0.3)
ax3 = plt.subplot2grid((2, 2), (1, 1), colspan=1)
ax3.plot( 'x','z', data=df, marker='o', color="orange", alpha=0.3)
plt.show()

 Python画散点图

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })
ax1 = plt.subplot2grid((2, 4), (0, 0), colspan=4)
ax1.plot( 'x', 'y', data=df, marker='o', alpha=0.4)
ax2 = plt.subplot2grid((2, 4), (1, 0), colspan=3)
ax2.plot( 'x','z', data=df, marker='o', color="grey", alpha=0.3)
ax3 = plt.subplot2grid((2, 4), (1, 3), colspan=1)
ax3.plot( 'x','z', data=df, marker='o', color="orange", alpha=0.3)
plt.show()

 Python画散点图

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })
ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=1)
ax1.plot( 'x', 'y', data=df, marker='o', alpha=0.4)
ax2 = plt.subplot2grid((2, 2), (1, 0), colspan=1)
ax2.plot( 'x','z', data=df, marker='o', color="grey", alpha=0.3)
ax3 = plt.subplot2grid((2, 2), (0, 1), rowspan=2)
ax3.plot( 'x','z', data=df, marker='o', color="orange", alpha=0.3)
plt.show()

 Python画散点图

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101), 'z': (np.random.randn(100)*15+range(1,101))*2 })

plt.plot( 'x', 'y', data=df, marker='o', color='mediumvioletred')
plt.show()

 Python画散点图

本博主新开公众号, 希望大家能扫码关注一下,十分感谢大家。

Python画散点图 

本文来自:https://github.com/holtzy/The-Python-Graph-Gallery/blob/master/PGG_notebook.py 

本文地址:https://blog.csdn.net/weixin_41869644/article/details/107449036