Anonymous
Создайте дугу между двумя точками в matplotlib
Сообщение
Anonymous » 15 ноя 2024, 09:32
Я пытаюсь воссоздать диаграмму ниже, используя matplotlib:
Я почти все сделал, но не могу понять, как создать дуги между годами:
Код: Выделить всё
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
import numpy as np
import pandas as pd
colors = ["#CC5A43","#2C324F","#5375D4",]
data = {
"year": [2004, 2022, 2004, 2022, 2004, 2022],
"countries" : [ "Denmark", "Denmark", "Norway", "Norway","Sweden", "Sweden",],
"sites": [4,10,5,8,13,15]
}
df= pd.DataFrame(data)
df = df.sort_values([ 'year'], ascending=True ).reset_index(drop=True)
df['ctry_code'] = df.countries.astype(str).str[:2].astype(str).str.upper()
df['year_lbl'] ="'"+df['year'].astype(str).str[-2:].astype(str)
sites = df.sites
lbl1 = df.year_lbl
fig, ax = plt.subplots( figsize=(6,6),sharex=True, sharey=True, facecolor = "#FFFFFF", zorder= 1)
ax.scatter(sites, sites, s= 340, c= colors*2 , zorder = 1)
ax.set_xlim(0, sites.max()+3)
ax.set_ylim(0, sites.max()+3)
ax.axline([ax.get_xlim()[0], ax.get_ylim()[0]], [ax.get_xlim()[1], ax.get_ylim()[1]], zorder = 0, color ="#DBDEE0" )
for i, l1 in zip(range(0,6), lbl1) :
ax.annotate(l1, (sites[i], sites[i]), color = "w",va= "center", ha = "center")
ax.set_axis_off()
Что дает мне следующее:
< /p>
Я пробовал и mpatches.arc, и patches, и path, но не получилось.
Подробнее здесь:
https://stackoverflow.com/questions/779 ... matplotlib
1731652378
Anonymous
Я пытаюсь воссоздать диаграмму ниже, используя matplotlib: [img]https://i.sstatic.net/P9NGe.png[/img] Я почти все сделал, но не могу понять, как создать дуги между годами: [code]import matplotlib.pyplot as plt from scipy.interpolate import interp1d import numpy as np import pandas as pd colors = ["#CC5A43","#2C324F","#5375D4",] data = { "year": [2004, 2022, 2004, 2022, 2004, 2022], "countries" : [ "Denmark", "Denmark", "Norway", "Norway","Sweden", "Sweden",], "sites": [4,10,5,8,13,15] } df= pd.DataFrame(data) df = df.sort_values([ 'year'], ascending=True ).reset_index(drop=True) df['ctry_code'] = df.countries.astype(str).str[:2].astype(str).str.upper() df['year_lbl'] ="'"+df['year'].astype(str).str[-2:].astype(str) sites = df.sites lbl1 = df.year_lbl fig, ax = plt.subplots( figsize=(6,6),sharex=True, sharey=True, facecolor = "#FFFFFF", zorder= 1) ax.scatter(sites, sites, s= 340, c= colors*2 , zorder = 1) ax.set_xlim(0, sites.max()+3) ax.set_ylim(0, sites.max()+3) ax.axline([ax.get_xlim()[0], ax.get_ylim()[0]], [ax.get_xlim()[1], ax.get_ylim()[1]], zorder = 0, color ="#DBDEE0" ) for i, l1 in zip(range(0,6), lbl1) : ax.annotate(l1, (sites[i], sites[i]), color = "w",va= "center", ha = "center") ax.set_axis_off() [/code] Что дает мне следующее: [img]https://i.sstatic.net/UaONB.png[/img] < /p> Я пробовал и mpatches.arc, и patches, и path, но не получилось. Подробнее здесь: [url]https://stackoverflow.com/questions/77942347/create-an-arc-between-two-points-in-matplotlib[/url]